I have 2 resources: Product and Inventory (BelongsToMany relationship)
The pivot table (inventory_product
) has an extra field is quantity
(Storing quantity of the product in an inventory).
When using BelongToMany field, it works correctly but the dropdown list still shows all the possible options even the ones already attached. Therefore, I use relatableQuery method to exclude attached ones from the query.
public static function relatableQuery(NovaRequest $request, $query)
{
// Get the current resource.
$resourceId = $request->route()->parameter('resourceId');
// Get the array of product ids that already attached.
$attachedItems = app(self::$model)->whereHas('inventories', function ($query) use ($resourceId) {
$query->where('inventory_id', $resourceId);
})->get()->pluck('id');
return $query->whereNotIn('id', $attachedItems);
}
But the problem is when I go to the edit form to adjust the quantity
field it doesn't allow me to do that because of the validation. The dropdown menu is empty because above code filtered them. So how can I avoid the above code in edit form view?
I already tried to get the parameter
from the NovaRequest
, but it didn't work :(
Thank you for your help!!!