Laravel Backpack select_and_order: how to make options list dependent on model being edited
I have a select_and_order field that works in my
use App\Models\Tool;
class ProjectCrudController extends CrudController
{
protected function setupCreateOperation()
{
CRUD::addField([
'name' => 'tools'
'type' => 'select_and_order',
'options' => Tool::get()->pluck('selector','id'),
]);
Now the possible tools are dependent on the type of project, so instead of returning all tools to options
as above, I would like something like
CRUD::addField([
'name' => 'tools'
'type' => 'select_and_order',
'options' => Tool
::projectTypeFilter($this->crud->model->type)
->pluck('selector','id'),
]);
But the new query returns []
because - as far as I can see - $this->crud->model
is not hydrated.
protected function setupUpdateOperation()
{
CRUD::addField([
'name' => 'naam',
'type' => 'select_and_order',
'options' => dump($this->crud->getModel()),
]);
}
shows
App\Models\Project {#1779 ▼
#guarded: array:1 [▶]
#fakeColumns: array:1 [▶]
#casts: array:4 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
How can I have the model hydrated?
I think what you're looking for is not $this->crud->getModel()
(which will get you the Eloquent model class), but $this->crud->getCurrentEntry()
which will get you:
-
false
, if the model has not been hydrate (eg. insidesetupCreateOperation()
, where there is no current entry to speak of); - the current entry for that Eloquent model, if present (eg. inside
setupUpdateOperation()
;