-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #568 from Laravel-Backpack/nested-crud-example
Add a nested crud example
- Loading branch information
Showing
4 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/Http/Controllers/Admin/PetShop/OwnerPetsCrudController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters