0

I have a collection that is returned as such:

$scheduleLoads = Load::with('shipment','driver','tractor');

Now, my question is related to the with issue - is there a way to add the relationships of these relationships into my returned collection?

For example:

In the Load model I have the following relationship to shipment:

public function shipment(){
    return $this->belongsTo(shipment::class, 'shipmentID');
}

In the shipment model I have the following relationship:

public function shiptoAccount(){
    return $this->belongsTo('App\Customer', 'ship_to');
}

Is there a way to include the shiptoAccount return of the shipment associated with the Loads collections?

6
  • Do you mean Load::with('shipment.shiptoAccount','driver','tractor');? Commented Sep 1, 2018 at 2:05
  • I'll add this above, but my relationship to the shipment is as such from my Load model: public function shipment(){ return $this->belongsTo(shipment::class, 'shipmentID'); } Commented Sep 1, 2018 at 2:11
  • Did you try my suggestion? Commented Sep 1, 2018 at 2:12
  • Wow, that made me feel a bit a bit dull... thank you so much! I was curious, just for future reference, how would I include another? Would I have to go out and write 'shipment.shiptoAccount','shipment.shipfromAccount', ... or some other way? Commented Sep 1, 2018 at 2:16
  • Yes, exactly like that. laravel.com/docs/5.6/eloquent-relationships#eager-loading Commented Sep 1, 2018 at 2:17

1 Answer 1

1

Use the "dot" syntax (documentation):

$scheduleLoads = Load::with('shipment.shiptoAccount', 'driver', 'tractor');
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.