Skip to content

Nested CRUDs example #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/Http/Controllers/Admin/CreatorCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
* Class CreatorCrudController.
*
* @property-read CrudPanel $crud
*/
class CreatorCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;

public function setup()
{
$this->crud->setModel('App\Models\Creator');
$this->crud->setRoute(config('backpack.base.route_prefix').'/creator');
$this->crud->setEntityNameStrings('creator', 'creators');
}

protected function setupListOperation()
{
$this->crud->addColumn('name');
$this->crud->addColumn([
'type' => 'relationship_count',
'name' => 'snippets',
'label' => 'Snippets',
'suffix' => ' snippets',
'link' => function ($entry) {
return backpack_url('creator/'.$entry->id.'/snippet');
},
]);
}
}
31 changes: 31 additions & 0 deletions app/Http/Controllers/Admin/CreatorSnippetCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
* Class SnippetCrudController.
*
* @property-read CrudPanel $crud
*/
class CreatorSnippetCrudController extends SnippetCrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

public function setup()
{
$user_id = \Route::current()->parameter('user_id');

$this->crud->setModel('App\Models\Snippet');
$this->crud->setRoute(config('backpack.base.route_prefix').'/creator/'.$user_id.'/snippet');
$this->crud->setEntityNameStrings('snippet', 'snippets');

// filter List operation (with search) to only show this users' entries
$this->crud->addClause('where', 'created_by', $user_id);
}
}
8 changes: 4 additions & 4 deletions app/Http/Controllers/Admin/MonsterCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,10 @@ protected function setupCreateOperation()
]);

$this->crud->addField([ // Browse multiple
'name' => 'browse_multiple',
'label' => 'Browse multiple',
'type' => 'browse_multiple',
'tab' => 'Uploads',
'name' => 'browse_multiple',
'label' => 'Browse multiple',
'type' => 'browse_multiple',
'tab' => 'Uploads',
'sortable' => true,
// 'multiple' => true, // enable/disable the multiple selection functionality
// 'mime_types' => null, // visible mime prefixes; ex. ['image'] or ['application/pdf']
Expand Down
36 changes: 36 additions & 0 deletions app/Http/Controllers/Admin/MySnippetCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
* Class SnippetCrudController.
*
* @property-read CrudPanel $crud
*/
class MySnippetCrudController extends SnippetCrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

public function setup()
{
$this->crud->setModel('App\Models\Snippet');
$this->crud->setRoute(config('backpack.base.route_prefix').'/my-snippet');
$this->crud->setEntityNameStrings('snippet', 'snippets');

// filter List operation (with search) to only show this users' entries
$this->crud->addClause('where', 'created_by', backpack_auth()->user()->id);

// if the user tries to access somone else's entries, block him
$entry = $this->crud->getCurrentEntry();

if ($entry && $entry->created_by != backpack_auth()->user()->id) {
abort(403, "You don't have access to this entry.");
}
}
}
102 changes: 102 additions & 0 deletions app/Http/Controllers/Admin/SnippetCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\SnippetRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
* Class SnippetCrudController.
*
* @property-read CrudPanel $crud
*/
class SnippetCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

public function setup()
{
$this->crud->setModel('App\Models\Snippet');
$this->crud->setRoute(config('backpack.base.route_prefix').'/snippet');
$this->crud->setEntityNameStrings('snippet', 'snippets');
}

protected function setupListOperation()
{
$this->crud->addColumn('name');
$this->crud->addColumn([
'label' => 'Category',
'type' => 'select',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
]);
$this->crud->addColumn([
'label' => 'Created by',
'type' => 'select',
'name' => 'created_by',
'entity' => 'creator',
'attribute' => 'name',
]);

$this->crud->addColumn([
'label' => 'Updated by',
'type' => 'select',
'name' => 'updated_by',
'entity' => 'updater',
'attribute' => 'name',
]);
}

protected function setupShowOperation()
{
$this->setupListOperation();

$this->crud->addColumn('description');
$this->crud->addColumn('content');
}

protected function setupCreateOperation()
{
$this->crud->setValidation(SnippetRequest::class);

$this->crud->addField([
'type' => 'text',
'name' => 'name',
'label' => 'Name',
'wrapperAttributes' => [
'class' => 'form-group col-md-6',
],
]);
$this->crud->addField([
'label' => 'Category',
'type' => 'select',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
'wrapperAttributes' => [
'class' => 'form-group col-md-6',
],
]);
$this->crud->addField([
'type' => 'simplemde',
'name' => 'description',
'label' => 'Description',
]);
$this->crud->addField([
'type' => 'textarea',
'name' => 'content',
'label' => 'Content',
]);
}

protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}
55 changes: 55 additions & 0 deletions app/Http/Requests/CreatorRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreatorRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return backpack_auth()->check();
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
// 'name' => 'required|min:5|max:255'
];
}

/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}

/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
//
];
}
}
56 changes: 56 additions & 0 deletions app/Http/Requests/SnippetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SnippetRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return backpack_auth()->check();
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|min:5|max:255',
'category_id' => 'required',
];
}

/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}

/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
//
];
}
}
25 changes: 25 additions & 0 deletions app/Models/Creator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Backpack\CRUD\app\Models\Traits\InheritsRelationsFromParentModel;

class Creator extends BackpackUser
{
use InheritsRelationsFromParentModel;
use CrudTrait;

protected $table = 'users';

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

public function snippets()
{
return $this->hasMany('App\Models\Snippet', 'created_by');
}
}
Loading