@artem_atlas

Почему laravel пытается обратиться к столбцу из другой таблицы?

При отображении таблицы отображается ошибка:
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)

Перевод:
SQLSTATE [42S22]: Столбец не найден: 1054 Неизвестный столбец 'office_to_do_projects.project_id' в 'предложении where' (SQL: выберите * из `office_to_do_projects`, где` office_to_do_projects``project_id` = 1 и `office_to_do_projects``project_id` равно не равно NULL, а `office_to_do_projects` .deleted_at` равно нулю)

ругается на отображение количества задач:
@foreach($projects as $project)
      ***
      <td>{{ count($project->tasks()->get()) }}</td>
      ***

Контроллер:
$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:
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:
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:
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');
    }


Мои таблицы:
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();
  • Вопрос задан
  • 122 просмотра
Решения вопроса 1
part_os
@part_os
Сложное в простом
protected $table = 'office_to_do_projects';
У вас либо нейминг таблиц одинаковый, либо вы два раза одно и тоже в вопрос скопировали.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы