Skip to content

Commit 934340d

Browse files
authored
Merge pull request #568 from Laravel-Backpack/nested-crud-example
Add a nested crud example
2 parents bcfc45e + b7cde6b commit 934340d

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

app/Http/Controllers/Admin/PetShop/OwnerCrudController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ protected function setupListOperation()
4949
{
5050
CRUD::column('name');
5151
CRUD::column('avatar.url')->type('image')->label('Avatar');
52+
53+
CRUD::button('view_pets')->stack('line')->view('crud::buttons.quick')->meta([
54+
'access' => true,
55+
'label' => 'View Pets',
56+
'icon' => 'la la-paw',
57+
'wrapper' => [
58+
'href' => function ($entry, $crud) {
59+
return url($crud->route.'/'.$entry->getKey().'/pets');
60+
},
61+
'title' => 'view owner pets',
62+
],
63+
]);
5264
}
5365

5466
/**
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin\PetShop;
4+
5+
use Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
6+
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
7+
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
8+
use Illuminate\Database\Eloquent\Builder;
9+
10+
class OwnerPetsCrudController extends PetCrudController
11+
{
12+
use CreateOperation {store as traitStore; }
13+
use UpdateOperation {update as traitUpdate; }
14+
15+
private int $owner;
16+
17+
/**
18+
* Configure the CrudPanel object. Apply settings to all operations.
19+
*
20+
* @return void
21+
*/
22+
public function setup()
23+
{
24+
parent::setup();
25+
26+
// get the owner parameter
27+
$this->owner = \Route::current()->parameter('owner');
28+
29+
// set a different route for the admin panel
30+
CRUD::setRoute(config('backpack.base.route_prefix').'/pet-shop/owner/'.$this->owner.'/pets');
31+
32+
// show only that owner's pets
33+
CRUD::addBaseClause(function (Builder $query) {
34+
$query->whereHas('owners', function (Builder $query) {
35+
$query->where('owner_id', $this->owner)
36+
->where('role', 'Owner');
37+
});
38+
});
39+
}
40+
41+
protected function setupCreateOperation()
42+
{
43+
parent::setupCreateOperation();
44+
// remove the relationship field
45+
CRUD::removeField('owners');
46+
// add an hidden field so that our stripRequest allow that key to go through.
47+
CRUD::field('owners')->type('hidden');
48+
}
49+
50+
protected function setupUpdateOperation()
51+
{
52+
parent::setupUpdateOperation();
53+
// remove the relationship field
54+
CRUD::removeField('owners');
55+
// add an hidden field so that our stripRequest allow that key to go through.
56+
CRUD::field('owners')->type('hidden');
57+
}
58+
59+
public function store()
60+
{
61+
CRUD::setRequest($this->addOwnerToRequest());
62+
63+
return $this->traitStore();
64+
}
65+
66+
public function update()
67+
{
68+
CRUD::setRequest($this->addOwnerToRequest());
69+
70+
return $this->traitUpdate();
71+
}
72+
73+
private function addOwnerToRequest()
74+
{
75+
return CRUD::getRequest()->merge([
76+
'owners' => [
77+
[
78+
'owners' => $this->owner,
79+
'role' => 'Owner',
80+
],
81+
],
82+
]);
83+
}
84+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"require": {
88
"php": "^8.1",
99
"laravel/framework": "^10.0",
10-
"backpack/crud": "^6.0",
10+
"backpack/crud": "^6.1.13",
1111
"backpack/pro": "^2.0",
1212
"backpack/filemanager": "^3.0",
1313
"backpack/theme-coreuiv2": "^1.0",

routes/backpack/custom.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@
7979
Route::get('about', function () {
8080
return view('admin.petshop_about');
8181
});
82-
Route::crud('owner', 'OwnerCrudController');
8382
Route::crud('invoice', 'InvoiceCrudController');
8483
Route::crud('pet', 'PetCrudController');
8584
Route::crud('passport', 'PassportCrudController');
8685
Route::crud('skill', 'SkillCrudController');
8786
Route::crud('comment', 'CommentCrudController');
8887
Route::crud('badge', 'BadgeCrudController');
88+
Route::crud('owner', 'OwnerCrudController');
89+
// nested crud panel for owner pets
90+
Route::group(['prefix' => 'owner/{owner}'], function () {
91+
Route::crud('pets', 'OwnerPetsCrudController');
92+
});
8993
});
9094
}); // this should be the absolute last line of this file

0 commit comments

Comments
 (0)