CRUD Traits

There are traits to help build up common functionality on your Cruds and Controllers

PublishesItems

Usage

Add this trait to your Crud controllers. It will provide functionality to work with Publish and Unpublish bulk actions.

You will then need to add routes for this functionality. The Route names should be based off of the route setting that you will have set in the Crud, with publish and unpublish as suffixes. If you had configured your Crud with ->route('admin.pages'), you would set them up as:

Route::post('admin/case-studies/publish', 'Admin\CaseStudyController@publish')->name('admin.case-studies.publish');
Route::post('admin/case-studies/unpublish', 'Admin\CaseStudyController@unpublish')->name('admin.case-studies.unpublish');

Optional / Extension

You may with to limit publishing of items to happen only when specific criteria are met. You can add a public canPublish() method to the Model that the Controller deals with. This should return a boolean value which defines whether the current state of the model is a valid state to publish.

You can also add a public $publish_criteria to the Model. This should be a string that describes what is required to publish that model. This message will be returned in if publishing fails.

Note: Publishing items is currently an all-or-nothing process. If you select multiple items and one of them fails, none of the selected items will be published. As such, it would be good to create a boolean column for the index that shows whether or not the item can be published.


IsPublishableCrud

Usage

Add this trait to your Crud that handles a Publishable Model. It will provide functionality to quickly set up Publishing-based bulk actions and filters.

You can then quickly add a Filter with:

$this->setItemFilters([
    'published' => $this->getPublishingFilter(),
]);

and Bulk actions with:

$this->setBulkActions([
    "actions" => $this->getPublishingBulkActions(),
]);

Optional / Extension

If you want to change the options available in the filter (for example, you don't care about items with a publish_at date for the filters), you should override getPublishedOptions and filterPublishedState to update it's functionality.

HasUuids

This trait can be added to models to cause them to generate UUID values in specified columns as part of the model creating event.

Usage

Once installed, you can then add this trait to Models that should have a uuid column.

You should create a uuid column as part of migration.

$table->uuid('uuid')->unique();

You should then add a protected $uuid_column property that is the name of column which should generate a uuid. The most likely use-case is:

protected $uuid_column = 'uuid';

Warning

It it not recommended that you add this after you already have a table set up and in-use, especially if the site is already live without it. If you are adding this as an update:

  1. The table column must also be ->nullable().
  2. Due to the fact that this is tied to the creating event hook, you will need to create additional steps to ensure that existing items have their uuid columns populated.