Ad

How Do I Select Rows That Their Relationship Models Attribute Is Equals True In Laravel 5.1?

- 1 answer

I have a the model Cart with the attributes id, clientIp, completed. I have another model that called Item. Item Model has a belongsTo relationship to Cart Model. and the Cart Model has hasMany relationship to Item model. so far so good.

How can I fetch all the $items that their $cart->completed is true?

I'm really sorry if I haven't explained well my question, I tried my best.

Ad

Answer

You can try this..

$items = Item::whereHas('cart', function($q) { 
                 $q->where('completed', '=', 'true')
              })->get();
Ad
source: stackoverflow.com
Ad