Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisradermacher's avatar

Eloquent relationship hasManyThrough

Hi there, I'm new to Laravel and Laracasts so apologies for that in advance ..

I have three tables: dealers, addresses and orders. I want to add a method "delivery_addresses" to the Dealer model. The orders table owns both columns required for the relationship: orders.dealer_id and orders.delivery_address_id.

My attempts at using hasManyThrough are failing I think because Laravel wants a foreign key column on the addresses table but I have none.

Thanks for the help!

0 likes
1 reply
chrisradermacher's avatar

I think I solved this myself by adding a foreign key addresses.dealer_id which points to dealers.id. Initially I didn't want to have to change the table structure but oh well. This now works


	// Dealer owns orders
	// addresses are assigned to orders
	public function delivery_addresses()
	{
		return $this->hasManyThrough(
			'\App\Models\Address', // content we want to load (target table)
			'\App\Models\Order', // the "helper" (through) table
			'delivery_address_id',
			'dealer_id', 
		);
	}

Please or to participate in this conversation.