A
A
Artem2020-06-16 16:08:07
Laravel
Artem, 2020-06-16 16:08:07

Why is laravel trying to access a column from another table?

When displaying the table, an error is displayed:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'office_to_do_projects.project_id' in 'where clause' (SQL: select * from `office_to_do_projects` where `office_to_do_projects`.`project_id`=1 and `office_to_do_projects`.`project_id` is not null and `office_to_do_projects`.`deleted_at` is null) (View: C:\OSPanel\domains\vs.develop\resources\views\page\Office\ToDo\Projects.blade.php)

Translation:
SQLSTATE [42S22]: Column not found: 1054 Unknown column 'office_to_do_projects.project_id' in 'where' clause (SQL: select * from `office_to_do_projects` where `office_to_do_projects``project_id` = 1 and `office_to_do_projects``project_id` equals not is NULL and `office_to_do_projects` .deleted_at` is nil)

swears at displaying the number of tasks:
@foreach($projects as $project)
      ***
      <td>{{ count($project->tasks()->get()) }}</td>
      ***

Controller:
$user = Auth::user();
        $projects = Auth::user()->toDoProjects()->orderby('created_at')->get();
        $currenttime = Carbon::now()->format('h:i a');
        $today = Carbon::now()->formatLocalized('%a %d %b %y');
        return view('page.Office.ToDo.Projects', compact('projects', 'currenttime', 'today', 'user'));

User model:
public function toDoProjects(){
        return $this->hasMany('App\Models\Office\ToDo\Project');
    }

    public function toDoTasks(){
        return $this->hasManyThrough('App\Models\Office\ToDo\Task', 'App\Models\Office\ToDo\Project');
    }


Project Model:
use SoftDeletes;

    protected $table = 'office_to_do_projects';

    protected $dates = ['deleted_at'];

    protected $fillable = [
        'name',
        'slug',
        'desc',
        'duedate',
        'completed'
    ];

    public function setDuedateAttribute($date){
        $this->attributes['duedate'] = Carbon::parse($date);
    }

    public function user(){
        return $this->belongsTo('App\User');
    }

    public function tasks(){
        return $this->hasMany('App\Models\Office\ToDo\Task');
    }

    public function subtasks(){
        return $this->hasManyThrough('App\Models\Office\ToDo\Subtask', 'App\Models\Office\ToDo\Task');
    }


Task Model:
use SoftDeletes;

    protected $table = 'office_to_do_projects';

    protected $dates = ['deleted_at'];

    protected $fillable = [
        'name',
        'slug',
        'desc',
        'duedate',
        'completed'
    ];

    public function setDuedateAttribute($date){
        $this->attributes['duedate'] = Carbon::parse($date);
    }

    public function user(){
        return $this->belongsTo('App\User');
    }

    public function project(){
        return $this->belongsTo('App\Models\Office\ToDo\Project');
    }

    public function subtasks(){
        return $this->hasMany('App\Models\Office\ToDo\Subtask');
    }


My tables:
office_to_do_projects:
$table->bigIncrements('id');

            $table->bigInteger('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->string('name'); 
            $table->string('slug');
            $table->longText('desc'); 
            $table->date('duedate');
            $table->boolean('completed')->default(false);
            $table->timestamps();
            $table->softDeletes();


office_to_do_tasks:
$table->bigIncrements('id');

            $table->bigInteger('project_id')->unsigned()->index();
            $table->foreign('project_id')->references('id')->on('office_to_do_projects')->onDelete('cascade');

            $table->string('name');
            $table->string('slug');
            $table->longText('desc');
            $table->date('duedate');
            $table->boolean('completed')->default(false);
            $table->timestamps();
            $table->softDeletes();


office_to_do_subtasks:
$table->bigIncrements('id');

            $table->bigInteger('task_id')->unsigned()->index();
            $table->foreign('task_id')->references('id')->on('office_to_do_tasks')->onDelete('cascade');

            $table->string('name');
            $table->string('slug');
            $table->longText('desc');
            $table->date('duedate'); 
            $table->boolean('completed')->default(false);
            $table->timestamps();
            $table->softDeletes();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
part_os, 2020-06-18
@artem_atlas

protected $table = 'office_to_do_projects';
You either have the same table naming, or you copied the same thing twice into the question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question