crud->addClause(function (Builder $builder){ $builder->whereHas('category', function ($query) { $query->where('user_id', backpack_user()->id); }); }); } /** * Define what happens when the List operation is loaded. * * @see https://backpackforlaravel.com/docs/crud-operation-list-entries * @return void */ protected function setupListOperation() { // CRUD::column([ // Select // 'label' => "Category", // 'type' => 'select', // 'name' => 'category_id', // the db column for the foreign key // 'entity' => 'category', // 'model' => "App\Models\Category", // related model // 'attribute' => 'name', // foreign key attribute that is shown to user // 'priority' => 2, // 'options' => (function ($query) { // return $query->orderBy('id', 'ASC')->get(); // }), // you can use this to filter the results show in the select // ]); CRUD::setFromDb(); // set columns from db columns. $this->crud->removeColumn('category_id'); $this->crud->addColumn([ // Select 'label' => "Category", 'type' => 'select', 'name' => 'category_id', // the db column for the foreign key 'entity' => 'category', 'model' => "App\Models\Category", // related model 'attribute' => 'name', // foreign key attribute that is shown to user 'options' => (function ($query) { return $query->orderBy('id', 'ASC')->get(); }), // you can use this to filter the results show in the select ]); // dd($this->crud); // dd($this->settings); /** * Columns can be defined using the fluent syntax: * - CRUD::column('price')->type('number'); */ } /** * Define what happens when the Create operation is loaded. * * @see https://backpackforlaravel.com/docs/crud-operation-create * @return void */ protected function setupCreateOperation() { CRUD::setFromDb(); // set fields from db columns. CRUD::field([ // Select 'label' => "Category", 'type' => 'select', 'name' => 'category_id', // the db column for the foreign key 'entity' => 'category', 'model' => "App\Models\Category", // related model 'attribute' => 'name', // foreign key attribute that is shown to user 'options' => (function ($query) { return $query->orderBy('id', 'ASC')->get(); }), // you can use this to filter the results show in the select ]); /** * Fields can be defined using the fluent syntax: * - CRUD::field('price')->type('number'); */ } /** * Define what happens when the Update operation is loaded. * * @see https://backpackforlaravel.com/docs/crud-operation-update * @return void */ protected function setupUpdateOperation() { $this->setupCreateOperation(); } }