I'm trying to provide a Select
list with only records that are related to the model via a pivot table.
While building a time tracker/budgeting software for a client I have two models I'm working with called Budgets and Projects that are joined together with a pivot table. (So budgets
, projects
, and budget_project
)
I'm trying to display all projects that are related to a selected Budget
(from the Budget
resource when calling an action) on a Select
field. I can't figure out how to pass the model->id
into the fields function. I will then be running some code to analyze the Projects
associated with the given Budget
and creating a bunch of records that extend across the date range and other relationships.
Please help!
I'm looking for something like this...
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),
];
}
}