1
votes

Title

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause' (SQL: select * from outcomes where (user_id = 37 and todos.outcome_id = outcomes.id) and outcomes.deleted_at is null)

In my laravel project I want to get the list of all task there corresponding Outcome values but this gives an error

  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'));
    }

Here My Todo model


<?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();
    }


}

Outcome Model

<?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');
    }
}

Output I want basically enter image description here

2
pls, show us yours DB structure - Odin Thunder
@OdinThunder plz check my model I've edited the text. - Neeraj Tangariya
but this column is exist in database ? - aviboy2006

2 Answers

2
votes

If my suggestion right, you might get result with something like this:Brakcets are missing in previous so I've edited, Now this one is working what I want actually

$outcomes = Outcome::with(['todos' => function ($query) {
    $query->where('user_id', Auth::user()->id);
}])->get();
0
votes
  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'));
    }