Backpack for Laravel

Backpack for Laravel

A collection of Laravel packages to build a custom admin panel.

Documentation    Support

CrudController API

Here are all the functions you will need in order to build admin panels, sorted by how much you will most likely use them:

Always Used

<?php

$this->crud->setModel("App\Models\Example");
$this->crud->setRoute("admin/example");
// OR $this->crud->setRouteName("admin.example");
$this->crud->setEntityNameStrings("example", "examples");

$this->crud->setFromDb();  // automatically set fields and columns from your database columns  TODO: rephrase

Often Used

<?php

// ------ FIELDS (the last parameter is always the form - create/update/both)
$this->crud->addField($field_definition_array, 'update/create/both');
$this->crud->addField('db_column_name', 'update/create/both'); // a lazy way to add fields: let the CRUD decide what field type it is and set it automatically, along with the field label
$this->crud->addFields($array_of_fields_definition_arrays, 'update/create/both');
$this->crud->removeField('name', 'update/create/both');
$this->crud->removeFields($array_of_names, 'update/create/both');


// ------ COLUMNS
$this->crud->addColumn(); // add a single column, at the end of the stack
$this->crud->addColumns(); // add multiple columns, at the end of the stack
$this->crud->removeColumn('column_name'); // remove a column from the stack
$this->crud->removeColumns(['column_name_1', 'column_name_2']); // remove an array of columns from the stack
$this->crud->setColumnDetails('column_name', ['attribute' => 'value']);
$this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']);

$this->crud->setColumns(); // set the columns you want in the table view, either as array of column names, or multidimensional array with all columns detailed with their types


// ------ REORDER FIELDS
$this->crud->addField()->beforeField('name'); // will show this before the given field
$this->crud->addField()->afterField('name'); // will show this after the given field

// ------ REORDER COLUMNS
$this->crud->addColumn()->beforeColumn('name'); // will show this before the given column
$this->crud->addColumn()->afterColumn('name'); // will show this after the given column


// ------ CRUD BUTTONS
// possible positions: 'beginning' and 'end'; defaults to 'beginning' for the 'line' stack, 'end' for the others;
$this->crud->addButton($stack, $name, $type, $content, $position); // add a button; possible types are: view, model_function
$this->crud->addButtonFromModelFunction($stack, $name, $model_function_name, $position); // add a button whose HTML is returned by a method in the CRUD model
$this->crud->addButtonFromView($stack, $name, $view, $position); // add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons
$this->crud->removeButton($name);
$this->crud->removeButtonFromStack($name, $stack);


// ------ ACCESS
$this->crud->allowAccess('list');
$this->crud->allowAccess(['list', 'create', 'delete']);
$this->crud->denyAccess('list');
$this->crud->denyAccess(['list', 'create', 'delete']);

$this->crud->hasAccess('add'); // returns true/false
$this->crud->hasAccessOrFail('add'); // throws 403 error
$this->crud->hasAccessToAll(['create', 'update']); // returns true/false
$this->crud->hasAccessToAny(['create', 'update']); // returns true/false


// ------ REORDER
$this->crud->enableReorder('label_name', MAX_TREE_LEVEL);
// NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('reorder');

$this->crud->disableReorder();
$this->crud->isReorderEnabled(); // return true/false


// ------ DETAILS ROW
$this->crud->enableDetailsRow();
// NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('details_row');
// NOTE: you also need to do overwrite the showDetailsRow($id) method in your EntityCrudController to show whatever you'd like in the details row OR overwrite the views/backpack/crud/details_row.blade.php

$this->crud->disableDetailsRow();

// ------ REVISIONS
// You also need to use \Venturecraft\Revisionable\RevisionableTrait;
// Please check out: https://laravel-backpack.readme.io/docs/crud#revisions
$this->crud->allowAccess('revisions');

// ------ AJAX TABLE VIEW
// Please note the drawbacks of this though: 
// - 1-n and n-n relationship columns won't be searched anymore;
// - date and datetime columns won't be sortable anymore;
$this->crud->enableAjaxTable(); 

// ------ DATATABLE EXPORT BUTTONS
// Show export to PDF, CSV, XLS and Print buttons on the table view.
// Does not work well with AJAX datatables.
$this->crud->enableExportButtons();

// ------ ADVANCED QUERIES
$this->crud->addClause('active');
$this->crud->addClause('type', 'car');
$this->crud->addClause('where', 'name', '=', 'car');
$this->crud->addClause('whereName', 'car');
$this->crud->addClause('whereHas', 'posts', function($query) {
     $query->activePosts();
 });
$this->crud->orderBy();
$this->crud->groupBy();
$this->crud->limit();

Sometimes Used

<?php

$this->crud->setDefaultPageLength(10); // number of rows shown in table view
$this->crud->addButton($stack, $name, $type, $content); // add a button; possible types are: view, model_function
$this->crud->addButtonFromModelFunction($stack, $name, $model_function_name); // add a button whose HTML is returned by a method in the CRUD model
$this->crud->addButtonFromView($stack, $name, $view); // add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons
$this->crud->removeButton($name);
$this->crud->removeButtonFromStack($name, $stack);

// eager load a relationship
$this->crud->with('relationship_name');

// use a custom view for a CRUD operation
$this->crud->setShowView('your-view');
$this->crud->setEditView('your-view');
$this->crud->setCreateView('your-view');
$this->crud->setListView('your-view');
$this->crud->setReorderView('your-view');
$this->crud->setRevisionsView('your-view');
$this->crud->setRevisionsTimelineView('your-view');
$this->crud->setDetailsRowView('your-view');

Rarely Used

<?php

$this->crud->getEntry($entry_id);
$this->crud->getEntries();

$this->crud->getFields('create/update/both');

$this->crud->create($entry_request);
$this->crud->update($entry_id, $entry_request);
$this->crud->delete($entry_id);

Almost Never Used

<?php

$this->crud->updateTreeOrder($all_entries);

CrudController API