Anyone?
Laravel nova, throw exception in Observer
Hi,
How can I throw exception in an Observer, so the error will show in Nova? This works, but no errors/exception is thrown:
public function deleting(Wormgear $wormgear)
{
# Do not delete ordered products
if(Orderable::select('id')->where('orderable_id', $wormgear->id)->exists()) {
# stop delete
return false;
}
}
I have tried to throw an exception, but no error is displayed inside Nova.
use Illuminate\Validation\ValidationException;
throw ValidationException::withMessages([
'message' => 'not possible to delete ordered products'
]);
- assume model name is "Mmm"
- and you have a nova resource name “mmms” with the following fields
public function fields(Request $request) { return [ ID::make()->sortable(),
Text::make('Name', 'name')->sortable()->rules('required') ,
];
}
1- make observer for the “Mmm” model
php artisan make:observer MmmObserver --model=Mmm
2- attach Observer to model
In: App/Providers/AppServiceProvider.php
/** * Bootstrap any application services. * * @return void */ public function boot() { \App\Mmm::observe(\App\Observers\MmmObserver::class); }
3- throw an exception in your model Exception
In App/Observers/MmmObserver.php
public function creating(Mmm $mmm) { throw \Illuminate\Validation\ValidationException::withMessages([ 'name' => ['Errror Message 1'], ]);
}
!! Take cate that the ‘name’ key in “withMessages(” should match the file “name” specified in the resource !!
4- you should see error message when you submit the form
**sorry i could not do any better with the markdown formating :-) **
Please or to participate in this conversation.