Skip to content

Commit

Permalink
Merge pull request #568 from Laravel-Backpack/nested-crud-example
Browse files Browse the repository at this point in the history
Add a nested crud example
  • Loading branch information
pxpm authored Aug 22, 2023
2 parents bcfc45e + b7cde6b commit 934340d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/Http/Controllers/Admin/PetShop/OwnerCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ protected function setupListOperation()
{
CRUD::column('name');
CRUD::column('avatar.url')->type('image')->label('Avatar');

CRUD::button('view_pets')->stack('line')->view('crud::buttons.quick')->meta([
'access' => true,
'label' => 'View Pets',
'icon' => 'la la-paw',
'wrapper' => [
'href' => function ($entry, $crud) {
return url($crud->route.'/'.$entry->getKey().'/pets');
},
'title' => 'view owner pets',
],
]);
}

/**
Expand Down
84 changes: 84 additions & 0 deletions app/Http/Controllers/Admin/PetShop/OwnerPetsCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Http\Controllers\Admin\PetShop;

use Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
use Illuminate\Database\Eloquent\Builder;

class OwnerPetsCrudController extends PetCrudController
{
use CreateOperation {store as traitStore; }
use UpdateOperation {update as traitUpdate; }

private int $owner;

/**
* Configure the CrudPanel object. Apply settings to all operations.
*
* @return void
*/
public function setup()
{
parent::setup();

// get the owner parameter
$this->owner = \Route::current()->parameter('owner');

// set a different route for the admin panel
CRUD::setRoute(config('backpack.base.route_prefix').'/pet-shop/owner/'.$this->owner.'/pets');

// show only that owner's pets
CRUD::addBaseClause(function (Builder $query) {
$query->whereHas('owners', function (Builder $query) {
$query->where('owner_id', $this->owner)
->where('role', 'Owner');
});
});
}

protected function setupCreateOperation()
{
parent::setupCreateOperation();
// remove the relationship field
CRUD::removeField('owners');
// add an hidden field so that our stripRequest allow that key to go through.
CRUD::field('owners')->type('hidden');
}

protected function setupUpdateOperation()
{
parent::setupUpdateOperation();
// remove the relationship field
CRUD::removeField('owners');
// add an hidden field so that our stripRequest allow that key to go through.
CRUD::field('owners')->type('hidden');
}

public function store()
{
CRUD::setRequest($this->addOwnerToRequest());

return $this->traitStore();
}

public function update()
{
CRUD::setRequest($this->addOwnerToRequest());

return $this->traitUpdate();
}

private function addOwnerToRequest()
{
return CRUD::getRequest()->merge([
'owners' => [
[
'owners' => $this->owner,
'role' => 'Owner',
],
],
]);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"require": {
"php": "^8.1",
"laravel/framework": "^10.0",
"backpack/crud": "^6.0",
"backpack/crud": "^6.1.13",
"backpack/pro": "^2.0",
"backpack/filemanager": "^3.0",
"backpack/theme-coreuiv2": "^1.0",
Expand Down
6 changes: 5 additions & 1 deletion routes/backpack/custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@
Route::get('about', function () {
return view('admin.petshop_about');
});
Route::crud('owner', 'OwnerCrudController');
Route::crud('invoice', 'InvoiceCrudController');
Route::crud('pet', 'PetCrudController');
Route::crud('passport', 'PassportCrudController');
Route::crud('skill', 'SkillCrudController');
Route::crud('comment', 'CommentCrudController');
Route::crud('badge', 'BadgeCrudController');
Route::crud('owner', 'OwnerCrudController');
// nested crud panel for owner pets
Route::group(['prefix' => 'owner/{owner}'], function () {
Route::crud('pets', 'OwnerPetsCrudController');
});
});
}); // this should be the absolute last line of this file

0 comments on commit 934340d

Please sign in to comment.