Skip to content

Instantly share code, notes, and snippets.

@chriscalip
Created January 10, 2018 21:41
Show Gist options
  • Save chriscalip/8ce5c898e090336116bebb42fb095c94 to your computer and use it in GitHub Desktop.
Save chriscalip/8ce5c898e090336116bebb42fb095c94 to your computer and use it in GitHub Desktop.
<?php
namespace Backpack\NewsCRUD\app\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
// VALIDATION: change the requests to match your own file names if you need form validation
use Backpack\NewsCRUD\app\Http\Requests\ArticleRequest as StoreRequest;
use Backpack\NewsCRUD\app\Http\Requests\ArticleRequest as UpdateRequest;
class ArticleCrudController extends CrudController
{
public function __construct()
{
parent::__construct();
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
$this->crud->setModel("Backpack\NewsCRUD\app\Models\Article");
$this->crud->setRoute(config('backpack.base.route_prefix', 'admin').'/article');
$this->crud->setEntityNameStrings('article', 'articles');
/*
|--------------------------------------------------------------------------
| COLUMNS AND FIELDS
|--------------------------------------------------------------------------
*/
// ------ CRUD COLUMNS
$this->crud->addColumn([
'name' => 'date',
'label' => 'Date',
'type' => 'date',
]);
$this->crud->addColumn([
'name' => 'status',
'label' => 'Status',
]);
$this->crud->addColumn([
'name' => 'title',
'label' => 'Title',
]);
$this->crud->addColumn([
'name' => 'featured',
'label' => 'Featured',
'type' => 'check',
]);
$this->crud->addColumn([
'label' => 'Category',
'type' => 'select',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
'model' => "Backpack\NewsCRUD\app\Models\Category",
]);
// ------ CRUD FIELDS
$this->crud->addField([ // TEXT
'name' => 'title',
'label' => 'Title',
'type' => 'text',
'placeholder' => 'Your title here',
]);
$this->crud->addField([
'name' => 'slug',
'label' => 'Slug (URL)',
'type' => 'text',
'hint' => 'Will be automatically generated from your title, if left empty.',
// 'disabled' => 'disabled'
]);
$this->crud->addField([ // TEXT
'name' => 'date',
'label' => 'Date',
'type' => 'date',
'value' => date('Y-m-d'),
], 'create');
$this->crud->addField([ // TEXT
'name' => 'date',
'label' => 'Date',
'type' => 'date',
], 'update');
$this->crud->addField([ // WYSIWYG
'name' => 'content',
'label' => 'Content',
'type' => 'ckeditor',
'placeholder' => 'Your textarea text here',
]);
$this->crud->addField([ // Image
'name' => 'image',
'label' => 'Image',
'type' => 'browse',
]);
$this->crud->addField([ // SELECT
'label' => 'Category',
'type' => 'select2',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
'model' => "Backpack\NewsCRUD\app\Models\Category",
]);
$this->crud->addField([ // Select2Multiple = n-n relationship (with pivot table)
'label' => 'Tags',
'type' => 'select2_multiple',
'name' => 'tags', // the method that defines the relationship in your Model
'entity' => 'tags', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'model' => "Backpack\NewsCRUD\app\Models\Tag", // foreign key model
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
]);
$this->crud->addField([ // ENUM
'name' => 'status',
'label' => 'Status',
'type' => 'enum',
]);
$this->crud->addField([ // CHECKBOX
'name' => 'featured',
'label' => 'Featured item',
'type' => 'checkbox',
]);
$this->crud->enableAjaxTable();
}
public function store(StoreRequest $request)
{
return parent::storeCrud();
}
public function update(UpdateRequest $request)
{
return parent::updateCrud();
}
/**
* For illustration & debugging purposes.
* Override trait AjaxTable method search.
*/
public function search()
{
$this->crud->addClause('where', 'status', '=', 'PUBLISHED');
$this->crud->hasAccessOrFail('list');
$totalRows = $filteredRows = $this->crud->count();
// if a search term was present
if ($this->request->input('search') && $this->request->input('search')['value']) {
// filter the results accordingly
$this->crud->applySearchTerm($this->request->input('search')['value']);
// recalculate the number of filtered rows
$filteredRows = $this->crud->count();
}
// start the results according to the datatables pagination
if ($this->request->input('start')) {
$this->crud->skip($this->request->input('start'));
}
// limit the number of results according to the datatables pagination
if ($this->request->input('length')) {
$this->crud->take($this->request->input('length'));
}
// overwrite any order set in the setup() method with the datatables order
if ($this->request->input('order')) {
$column_number = $this->request->input('order')[0]['column'];
if ($this->crud->details_row) {
$column_number = $column_number - 1;
}
$column_direction = $this->request->input('order')[0]['dir'];
$column = $this->crud->findColumnById($column_number);
if ($column['tableColumn']) {
$this->crud->orderBy($column['name'], $column_direction);
}
}
$entries = $this->crud->getEntries();
return $this->crud->getEntriesAsJsonForDatatables($entries, $totalRows, $filteredRows);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment