Ray Coder
Ray Coder

Reputation: 1111

Cannot using where in eloquent

I'm using Eloquent ORM in Laravel. There is 2 model, User and Trans. User hasMany Trans, and Trans belongsTo User. The problem is when I'm using query where, it doesn't work.

I tried using ->get() in the last code, it still doesn't work. I tried using ->all() in the last code it still doesn't work. I tried whereIn, it still doesn't work.

User Model

public function trans()
{
    return $this->hasMany('App\Trans', 'user_id');
}

Trans Model

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

Controller

$trans = Auth::user()->trans->where('date', $date);

I want the output is based on query where the date is based on user input, when I delete ->where , it works and the output like this.

Collection {#797 ▼
  #items: array:13 [▼
    0 => Trans {#783 ▶}
    1 => Trans {#784 ▶}
    2 => Trans {#785 ▶}
    3 => Trans {#786 ▶}
  ]
}

Upvotes: 0

Views: 268

Answers (1)

Jignesh Joisar
Jignesh Joisar

Reputation: 15115

try to change like that

Auth::user()->trans->where('date', $date);

to

Auth::user()->trans()->where('date', $date)->get();

note : if you want to get only property then u get property without pointer but if you want to used another method then must used(add) pointer(->).

Upvotes: 1

Related Questions