【发布时间】:2023-04-07 18:51:01
【问题描述】:
标题
SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'user_id'(SQL:select * from
outcomeswhere (user_id= 37 and todos.outcome_id = results.id) 和 @ 987654325@.deleted_at为空)
在我的 laravel 项目中,我想获取所有任务的列表,其中包含相应的结果值,但这会产生错误
public function kanban()
{
$outcomes = Outcome::with('todos')
->where(function ($query) {
$query->select(DB::raw(1))
->from('todos')
->where('user_id','=',Auth::user()->id)
->whereRaw('todos.outcome_id = outcomes.id');
})
->get();
return view('taskmanagement.cruds.user.kanban',compact('outcomes'));
}
这是我的 Todo 模型
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Todo extends Model
{
use SoftDeletes;
//Table Name
protected $table = 'todos';
//Primary key
public $primaryKey = 'id';
protected $fillable = [
'todoable_id',
'todoable_type',
'title',
'description',
'admin_id',
'user_id',
'project_id',
'priority',
'outcome_id',
'tasktype_id',
'started_at',
'due_time',
'completed_at',
];
public function outcome() {
return $this->belongsTo('App\Outcome');
}
public function user() {
return $this->belongsTo('App\User');
}
public function todoable()
{
return $this->morphTo();
}
}
结果模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Outcome extends Model
{
use SoftDeletes;
//Table Name
protected $table = 'outcomes';
//Primary key
public $primaryKey = 'id';
protected $fillable = [
'name'
];
public function todos() {
return $this->hasMany('App\Todo');
}
}
【问题讨论】:
-
请给我们看看你的数据库结构
-
@OdinThunder 请检查我的模型我已经编辑了文本。
-
但该列存在于数据库中?