I have a few tables like so...
FreeLook
- id
SignVendor
- id
PaintVendor
- id
VendorJob
- vendor_id
- vendor_type
- free_look_id
- quote_id
Quote
- id
When a PaintVendor
or SignVendor
is chosen for a FreeLook
, a VendorJob
will be created and it has a polymorphic relationship so that I can use one table for both of the SignVendor
and PaintVendor
jobs. So, essentiall, I want to be able to do this...
FreeLook::find(1)->signVendorJob // gets the sign vendor job from VendorJob for that freelook
Here, I can't use a belongsTo
relationship because I cannot distinguish between a sign or paint vendor within the query (or at least I don't know of a way)
FreeLook::find(1)->signVendor // gets the sign vendor for that freelook
The problem is that I am getting the sign vendor through the VendorJob and Laravel doesn't have a hasOneThrough
FreeLook::find(1)->signVendor->quote // gets the sign vendor quote from VendorJob for that freelook
Since a SignVendor
can have more than one Quote
, I only want to get the quote related to the FreeLook
, or VendorJob
Am I going to need to write custom MySQL queries for all of these, or am I overcomplicating things, or just missing something?
Thanks for any help you can afford!
you'll use the morph
relation as mentioned in the doc.s
so in the VendorJob
model
// the morph relation
public function vendorable()
{
return $this->morphTo();
}
// the quote relation
public function quote()
{
return $this->hasOne('App\Quote');
}
and in PaintVendor
model
public function paintVendor()
{
return $this->morphMany('App\VendorJob', 'vendorable');
}
and the same for SignVendor
. now FreeLook::find(1)->signVendor
will return a list of all signVendor
records so you'll have to loop them like:
foreach(FreeLook::find(1)->signVendor as $vendor)
{
// fetching the quote of every signVendor related to FreeLook num. 1
$vendor->quote;
}
I hope this helps you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.