Search code examples
laravellaravel-nova

Get BelongsTo relation field with joined data in laravel nova v.3


I use laravel nova version 3.

I have a OrderItem model that related to a vendor through productVariation and product_vendor. So I define relationship as like as this in OrderItem model that returns a joined relationship:

public function vendor()
{
    return $this->belongsTo(ProductVariation::class)
        ->join('product_vendor', 'product_variation.product_vendor_id', '=', 'product_vendor.id')
        ->join('user', 'user.id', '=', 'product_vendor.user_id');
}

in OrederItemNovaResource I defined a BelongsTo field like this:

BelongsTo::make(__('Vendor'), 'vendor', \App\Nova\VendorNovaResource::class) 

also I check:

BelongsTo::make(__('Vendor'), 'vendor', \App\Admin\ProductVariationNovaResource::class)

But they don't returns joined data. How Can I receive correct relationship data from model instead of Nova resources fields?


Solution

  • Actually, this case will be handled by belongs-to-through relation that nova doesn't support currently. as other people have same problem with that, there is a Github package that you can use to make your belongsTo relation with multiple tables