【问题标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause'SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'user_id'
【发布时间】:2023-04-07 18:51:01
【问题描述】:

标题

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'user_id'(SQL:select * from outcomes where (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 请检查我的模型我已经编辑了文本。
  • 但该列存在于数据库中?

标签: html laravel eloquent


【解决方案1】:

如果我的建议是正确的,你可能会得到这样的结果:以前缺少括号,所以我已经编辑了,现在这个正在工作我真正想要的

$outcomes = Outcome::with(['todos' => function ($query) {
    $query->where('user_id', Auth::user()->id);
}])->get();

【讨论】:

  • 基本上我想要这样的输出imgur.com/Beb1OEa请打开这个链接
  • 您想要根据结果列出当前用户的待办事项列表,还是想要获得授权用户待办事项的结果列表?
  • 您的查询成功了,您只是错过了括号。现在它正在运行。谢谢朋友
  • 批准了你的编辑,对不起我的错误,我没有能力测试它。很高兴为您提供帮助)
【解决方案2】:
  public function kanban()
    {
        $outcomes = Outcome::with('todos')
        ->where(function ($query) {
            $query->select(DB::raw(1))
                  ->from('todos')
                  ->where('todos.user_id','=',Auth::user()->id)
                  ->whereRaw('todos.outcome_id','outcomes.id');
        })
        ->get();
        return view('taskmanagement.cruds.user.kanban',compact('outcomes'));
    }

【讨论】:

  • 试试这个答案。
  • @aviboy2006 我贴的这个和上面有什么区别?
  • -&gt;where('todos.user_id','=',Auth::user()-&gt;id) 这与使用别名访问列不同。在您中,您直接使用 user_id。 -&gt;where('user_id','=',Auth::user()-&gt;id)
  • @aviboy2006 仍然发生同样的错误基本上我想得到这样的输出,但问题是它显示了我只想显示当前记录的用户数据的所有待办事项。imgur.com/Beb1OEa
  • 你能确认数据库中存在列吗?
猜你喜欢
  • 2021-03-29
  • 2019-08-18
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 2020-06-08
  • 1970-01-01
相关资源
最近更新 更多