I have a branch, branch_products, and invoices, and invoice_products models.
branch has many branch_products, branch has many invoices,
invoices has many invoice_products branch_products has many invoice_products
Now going back to the question, When creating a new invoice product, I want the BelongsTo field to only show me branch products that is in the branch of the invoice.
// InvoiceProduct.php
public function invoice() {
return $this->belongsTo('App\Models\Invoice');
}
public function branchProduct() {
return $this->belongsTo('App\Models\BranchProduct');
}
// I need something like this
public function getAvailableBranchProducts() {
// get the branch id of the invoice, doesn't work
$branchId = $this->invoice->branch_id;
// get branch products that has the given branch id, doesn't work
return whereHas('branch_products', function ($query) use ($branchId) {
$query->where('branch_id', $branchId);
})->get();
}
I don't know how I would solve this. I've tried WhereHas, local scopes, but no luck.
I need it for Laravel Nova.