我试图提供一个Select列表,其中只有通过透视表与模型相关的记录。
在为客户构建一个时间跟踪器/预算软件时,我有两个正在使用的模型,称为预算和项目,它们与一个透视表结合在一起。(因此,budgetsprojectsbudget_project
我正试图在Budget字段上显示与选定Budget相关的所有项目(在调用操作时来自Select资源)。我不知道如何将model->id传递到fields函数中。然后,我将运行一些代码来分析与给定的Projects相关联的Budget,并创建一组跨越日期范围和其他关系的记录。
请帮忙!
我在找这样的东西…

class CreateSchedule extends Action
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        return Action::message('all good');
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        $budgetProject = BudgetProject::where('budget_id',$this->id)->get();

        foreach($budgetProject as $bp){
            $projectArray[$bp->project_id] = $bp->project->name;
        }

        return [
            Select::make('Project','project_id')->options($projectArray),
        ];
    }
}

最佳答案

public function fields(Request $request)
{
    $budgetProject = BudgetProject::where('budget_id',$request->budget_id)->get();

    dd($budgetProject);

    foreach($budgetProject as $bp){
        $projectArray[$bp->project_id] = $bp->project->name;
    }

    return [
        Select::make('Project','project_id')->options($projectArray),
    ];
}

关于php - 在Nova Action字段上运行where()-> get(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56535376/

10-12 07:18