Skip to content

Instantly share code, notes, and snippets.

@NoelDeMartin
Created September 22, 2019 09:19
Show Gist options
  • Save NoelDeMartin/fcc3dd15030c2137f2d5b7d871c73086 to your computer and use it in GitHub Desktop.
Save NoelDeMartin/fcc3dd15030c2137f2d5b7d871c73086 to your computer and use it in GitHub Desktop.
Laravel Nova resources boot method

This is an example on how to use boot methods in Nova Resources, similar to Eloquent Models.

In this case, I am rendering markdown to html before saving the model to the database (using Parsedown).

<?php
namespace App\Providers;
use Laravel\Nova\Nova;
use Laravel\Nova\Cards\Help;
use Illuminate\Support\Facades\Gate;
use Laravel\Nova\NovaApplicationServiceProvider;
class NovaServiceProvider extends NovaApplicationServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
parent::boot();
Nova::serving(function () {
$this->bootResources();
});
}
/**
* Register the Nova routes.
*
* @return void
*/
protected function routes()
{
Nova::routes()
->withAuthenticationRoutes()
->withPasswordResetRoutes()
->register();
}
/**
* Register the Nova gate.
*
* This gate determines who can access Nova in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewNova', function ($user) {
return in_array($user->email, [
//
]);
});
}
/**
* Get the cards that should be displayed on the default Nova dashboard.
*
* @return array
*/
protected function cards()
{
return [
new Help,
];
}
/**
* Get the extra dashboards that should be displayed on the Nova dashboard.
*
* @return array
*/
protected function dashboards()
{
return [];
}
/**
* Get the tools that should be listed in the Nova sidebar.
*
* @return array
*/
public function tools()
{
return [];
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Boot resources.
*
* @return void
*/
protected function bootResources()
{
foreach (Nova::$resources as $resource) {
if (!method_exists($resource, 'boot'))
continue;
$resource::boot();
}
}
}
<?php
namespace App\Nova;
use Illuminate\Http\Request;
use Parsedown;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Markdown;
use Laravel\Nova\Fields\Text;
use \App\Post as PostModel;
class Post extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = PostModel::class;
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'title';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'title', 'text_markdown',
];
/**
* Boot the resource.
*
* @return void
*/
public static function boot()
{
PostModel::saving(function ($post) {
$post->text_html = (new Parsedown)->text($post->text_markdown);
});
}
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Title')->sortable(),
Markdown::make('Text', 'text_markdown')->stacked(),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
@kennyray
Copy link

kennyray commented Nov 5, 2021

Nice! This might come in handy some day...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment