gpt4 book ai didi

php - Laravel Nova - 如何在创建新记录时过滤 BelongsTo 字段选择?

转载 作者:行者123 更新时间:2023-12-05 08:08:04 25 4
gpt4 key购买 nike

我有一个 branch、branch_products、invoices 和 invoice_products 模型。

branch有很多branch_products,分行有很多发票,

invoices 有很多 invoice_productsbranch_products 有很多 invoice_products

现在回到问题,创建新发票产品时,我希望 BelongsTo 字段仅显示发票分支中的分支产品。

// 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();
}

我不知道如何解决这个问题。我已经尝试过 WhereHas、本地范围,但没有成功。

我需要它用于 Laravel Nova。

最佳答案

您可以在 Nova 资源中添加一个方法:

例如,如果您有用户的 BelongsTo 字段并且需要按他们的类型过滤他们,您可以添加此方法:

    public static function relatableUsers(NovaRequest $request, $query){
return $query->where('type', 'editor');
}

关于php - Laravel Nova - 如何在创建新记录时过滤 BelongsTo 字段选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52804814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com