0

I have 4 models.

  • User
  • Shipment
  • Status
  • Status history

User.php

public function shipments() {
   return $this->hasMany('App\Shipment', 'from_user_id');
}

Shipment.php

public function statuses() {
   return $this->hasMany('App\StatusHistory', 'model_id');
}

A User has many Shipment. A Shipment has many Status instances through StatusHistory.

How can I get with an Eloquent relationship all the Shipment values for an User, that have a specific id value in the StatusHistory model?

2
  • 1
    There isn't enough information in your question to write an actual query. So I'd suggest looking at using whereHas when querying $user->shipments()->whereHas(...). Commented Feb 19, 2018 at 23:34
  • I just added some more information. So if I have $user->shipments, how can add extra query to that? Commented Feb 20, 2018 at 0:07

1 Answer 1

2

You can do

$user = User:find($id);
$shipments = $user->shipments()->whereHas('statuses', function ($query) {
    //Select all the shipments for this user where `StatusHistory` id is 1
    $query->where('id', 1);
})->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.