Skip to content

Instantly share code, notes, and snippets.

@tabacitu
Created June 15, 2023 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabacitu/aa7b3ecbe2c43c9bb4fa81433f6ba29f to your computer and use it in GitHub Desktop.
Save tabacitu/aa7b3ecbe2c43c9bb4fa81433f6ba29f to your computer and use it in GitHub Desktop.
Ban Operation Example
<?php
namespace App\Http\Controllers\Admin\Operations;
use Illuminate\Support\Facades\Route;
use Backpack\CRUD\app\Http\Controllers\Operations\Concerns\HasForm;
trait BanHistoryOperation
{
use HasForm;
/**
* Define which routes are needed for this operation.
*
* @param string $segment Name of the current entity (singular). Used as first URL segment.
* @param string $routeName Prefix of the route name.
* @param string $controller Name of the current CrudController.
*/
protected function setupBanHistoryRoutes($segment, $routeName, $controller)
{
Route::get($segment . '/{id}/ban-history', [
'as' => $routeName . '.banHistory',
'uses' => $controller . '@banHistory',
'operation' => 'banHistory',
]);
}
/**
* Method to handle the GET request and display the View with a Backpack form
*
* @param int $id
* @return \Illuminate\Contracts\View\View
*/
public function getBanHistoryForm(int $id = null)
{
$this->crud->hasAccessOrFail('banHistory');
// TODO: return a view with the ban history
dd('ban history');
}
}
<?php
namespace App\Http\Controllers\Admin\Operations;
use Backpack\CRUD\app\Http\Controllers\Operations\Concerns\HasForm;
trait BanOperation
{
use HasForm;
/**
* Define which routes are needed for this operation.
*
* @param string $segment Name of the current entity (singular). Used as first URL segment.
* @param string $routeName Prefix of the route name.
* @param string $controller Name of the current CrudController.
*/
protected function setupBanRoutes($segment, $routeName, $controller)
{
$this->formRoutes(
operationName: 'ban',
routesHaveIdSegment: true,
segment: $segment,
routeName: $routeName,
controller: $controller
);
}
/**
* Add the default settings, buttons, etc that this operation needs.
*/
protected function setupBanDefaults()
{
$this->formDefaults(
operationName: 'ban',
// buttonStack: 'line',
buttonMeta: [
'icon' => 'la la-ban',
// 'label' => 'Send Email',
]
);
}
/**
* Method to handle the GET request and display the View with a Backpack form
*
* @param int $id
* @return \Illuminate\Contracts\View\View
*/
public function getBanForm(int $id = null)
{
$this->crud->hasAccessOrFail('ban');
return $this->formView($id);
}
/**
* Method to handle the POST request and perform the operation
*
* @param int $id
* @return array|\Illuminate\Http\RedirectResponse
*/
public function postBanForm($id = null)
{
$this->crud->hasAccessOrFail('ban');
return $this->formAction($id, function ($inputs, $entry) {
// You logic goes here...
// dd('got to ' . __METHOD__, $inputs, $entry);
// show a success message
\Alert::success('User was banned!')->flash();
});
}
}
<?php
namespace App\Http\Controllers\Admin\Operations;
use Backpack\CRUD\app\Http\Controllers\Operations\Concerns\HasForm;
trait GeneralBanOperation
{
use HasForm;
/**
* Define which routes are needed for this operation.
*
* @param string $segment Name of the current entity (singular). Used as first URL segment.
* @param string $routeName Prefix of the route name.
* @param string $controller Name of the current CrudController.
*/
protected function setupGeneralBanRoutes($segment, $routeName, $controller)
{
$this->formRoutes(
operationName: 'generalBan',
routesHaveIdSegment: false,
segment: $segment,
routeName: $routeName,
controller: $controller
);
}
/**
* Add the default settings, buttons, etc that this operation needs.
*/
protected function setupGeneralBanDefaults()
{
$this->formDefaults(
operationName: 'generalBan',
buttonStack: 'top',
buttonMeta: [
'icon' => 'la la-ban',
]
);
}
/**
* Method to handle the GET request and display the View with a Backpack form
*
* @param int $id
* @return \Illuminate\Contracts\View\View
*/
public function getGeneralBanForm(int $id = null)
{
$this->crud->hasAccessOrFail('generalBan');
return $this->formView($id);
}
/**
* Method to handle the POST request and perform the operation
*
* @param int $id
* @return array|\Illuminate\Http\RedirectResponse
*/
public function postGeneralBanForm($id = null)
{
$this->crud->hasAccessOrFail('generalBan');
return $this->formAction($id, function ($inputs, $entry) {
// You logic goes here...
// dd('got to ' . __METHOD__, $inputs, $entry);
// show a success message
\Alert::success('User was banned!')->flash();
});
}
}
<?php
namespace App\Http\Controllers\Admin;
use Hash;
use App\Models\User;
use Backpack\CRUD\app\Library\CrudPanel\QuickButton;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
/**
* Class UserCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class UserCrudController extends CrudController
{
// ..
use \App\Http\Controllers\Admin\Operations\BanOperation;
use \App\Http\Controllers\Admin\Operations\GeneralBanOperation;
use \App\Http\Controllers\Admin\Operations\BanHistoryOperation;
// ...
protected function setupBanOperation()
{
CRUD::field('reason')->type('textarea');
}
protected function setupGeneralBanOperation()
{
CRUD::field('users')->type('select2_multiple')->model('App\Models\User')->attribute('name');
CRUD::field('reason')->type('textarea');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment