Search code examples
laraveleloquent-relationship

laravel query does not return the entire count of the rows of the relationship


i have a query that i want to return an object with its relationship, it has a many to one relationship, one order can have more than one drug i wrote a query like this

$orders = DrugRequest::where('user_id', $user_id)->with(['drugs'])->latest()->get();

this gets the relationship but not the full rows, for example if an order has two drugs it only returns the first drug not both,

this is my relationship

public function drugs(){ return $this->hasMany('App\OrderDrug' , 'order_id' , 'id');}

and this is when i dd($orders)

enter image description here

for the order object with the index 11 i have two drugs but it only returns one drug


Solution

  • i found the issue it was the query, i made the changes and it worked like this:

    $orders = DrugRequest::where('user_id', $user_id)->with('drugs')->latest()->get();