The basics
Thrust is a beautiful administration dashboard for Laravel applications. Of course, the primary feature of Nova is the ability to administer your underlying database records using Eloquent. Thrust accomplishes this by allowing you to define a Thrust "resource" that corresponds to each Eloquent model in your application.
Defining a resource
The resource is the main entity on thrust. It usually relates to a model in the database but can easily use any other repository.
Check this guide to
use non database models
.
Thrust resources must be stored at App/Thrust
folder and they will be automatically registered.
Once found, you can easily access this page with https://your.app/thrust/modelname
or where thrust
is the route prefix defined in config/thrust.php
file.
A simple model will look like the following, where the most important parts are defining the model
. In this case the Tax
. and the fields you want to display and edit.
namespace App\Thrust;
use BadChoice\Thrust\Fields\Text;
use BadChoice\Thrust\Resource;
class Tax extends Resource
{
public static $model = \App\Models\Config\Tax::class;
public function fields()
{
return [
Text::make('name')
];
}
}
Pagination
Since there are models that can have a large number of entities they are paginated. By default Thrust
paginates 25 objects, but you can simply change it overwritting the pagination
variable.
public $pagination = 50
Base query
Thrust
will fetch the database records using a simple Model::paginate()->get()
query, however in some cases you might want to apply a scope to it. You can easily achieve this by implementing the baseQuery
method.
protected function getBaseQuery()
{
return parent::getBaseQuery()->real()->where('id', '>', 1);
}
parent::getBaseQuery()
to keeps the pagination and search scopes.
Eager loading
Thrust
will find the relationships of the model based on the defined fields, however, you can fine tune it and provide you own eager loading relationships.
protected $with = ['group', 'group.printer', 'group.printerGroup', 'group.tax', 'modifierCategory', 'modifierGroup', 'printer', 'superGroup', 'printerGroup', 'tax'];
Search
Thrust
comes with a default search engine. You can define the fields that can be searchable by setting up the search
variable.
public static $search = ['name', 'employee.name'];
Sortable
Thrust
provides the sorting functionality out of the box, if your model can be manually sorted, you simply need to turn it on in your model.
public static $sortable = true;
By default it uses the order
field on your model, but you can change it by setting up the sortField
variable.
public static $sortField = 'manual_order';
Finally, as the table can be sorted by some fields, you have the option to allow to save the order when sorted.
This can be useful in cases you can sort by name, and then save this order to the database. Turn this feature on by setting the canSaveOrderWhenSorted
to true.
public static $canSaveOrderWhenSorted = true;
Title
Thrust
finds the title using the model name and appending the translations prefix defined in config/thrust.php
file. However, you can override the getTitle
function to define a custom title.
public function getTitle()
{
return __('admin.myTaxTitle');
}
Description
The same as title goes with the descriptions, it will search it using the translationsDescriptionsPrefix
translations prefix defined at the config/thrust.php
file. And it can be overrided with the getDescription
method.
public function getDescription()
{
return __('admin.myTaxTranslation');
}
Single resource
There are cases where you have only one record of a database model, for example the business settings.
In that case Thrust
won't show a list of models, but a full big edit page.
You can simply enable it by setting the singleResource
to true.
public static $singleResource = true;
Child resources
There are cases where you have a resource that always depends on a parent, like a comment that belongs to a post. In that case you can define a Child Resource
.
You must define the parentRelation
variable that should be a belongsTo
relationship in the model.
namespace App\Thrust;
use BadChoice\Thrust\ChildResource;
use BadChoice\Thrust\Fields\Text;
class Comment extends ChildResource
{
public static $model = \App\Models\Comment::class;
public static $search = ['body'];
public static $parentRelation = 'group';
public function fields()
{
return ([
Text::make('body')
]);
}
}
Custom routes
If you want to use a custom route, you can easily create a controller that returns the ThrustController
index method.
class TaxController extends Controller
{
public function index()
{
return (new ThrustController)->index('taxes');
}
}