How to hook into the delete event for a resource in laravel nova? - laravel

I have one query, I want to delete the image from the server when particular resource get deleted from Nova.
can anyone suggest me is there any way to override delete method for the resource.
EDIT: How to hook into the delete event for a resource in laravel nova?
Note: I know we can do using observer. but I am looking for another
way.

In order to hook into laravel nova's delete resource event, you don't have a builtin way. But the parent model's have a delete method, you can override it and do extra work there
//app/ParentModel.php
public function delete() {
/* add your extra logic for deleted model */
parent::delete();
}

you can use boot in your model like this:
public static function boot()
{
parent::boot();
self::deleted(function ($model) {
parent::remove($model, self::$index);
});
}

I used Observers and deleted function Nova Resource Events and works fine

Related

Updating a model property by POST API, can run an event?

The clear description is, an API with the post method updates a value of a model property. how I can set up a listener in Laravel that after every change by API, run an event for example send email or sms?
If you were wanting to create an event when any update happened you could easily just do this via the boot.
class SomeModel extends Model
{
public static function boot()
{
static::updated(function ($model) {
// send your email/sms
});
parent::boot();
}
}
However this would trigger when ever the model is updated, Since you only want this when you are updating via the API you would want to create a custom event and trigger it via dispatch in your api route/controller.
https://laravel.com/docs/8.x/events#dispatching-events

Laravel deleting cache working in controller but not in model closure

I'd like to delete a specific model from the cache using its id. This works as expected in the controller, but not using the model closure.
What I have in App\Models\Post:
use Illuminate\Support\Facades\Cache;
protected static function booted()
{
static::updated(function ($post) {
Cache::forget('post:'.$post->id);
});
}
If I do Cache::forget('post:'.$post->id); in the controller it works.
Something I'm missing?
Make sure that you are actually changing a value on your model, because the updated event only fires when the model was dirty, as you can see here.
The saved event however will fire whenever you call the save() method, as you can see here:
protected static function booted()
{
static::saved(function ($post) {
Cache::forget('post:'.$post->id);
});
}
From the docs:
The retrieved event will fire when an existing model is retrieved from
the database. When a new model is saved for the first time, the
creating and created events will fire. If a model already existed in
the database and the save method is called, the updating / updated
events will fire. However, in both cases, the saving / saved events
will fire.

How to use custom keys in laravel routes without scoping?

When using custom keys Laravel forces us with scoping, for example, I have a route to getting a country and a post
api/countries/{country:slug}/posts/{post:slug}
but I can't get that using slug key because it doesn't have a relation with country, and in this case, I want to handle scope myself and I don't need implicitly scope binding, but I get an error (Call to undefined method App\Country::posts() ).
so because of that I cant using this Laravel feature. is there a way to turn the implicitly scope binding off?
If the posts are not related to the countries, it may not make sense to nest them in the URI?
But, nonetheless, to answer your question, you need to do one of two things:
Instead of setting {country:slug}, just use {country} and then override getKeyRouteName() function on your Country and Post models.
Alternatively, especially if you want to use the ID elsewhere, use explicit model binding.
To use a slug without custom keys in the routes file
class Post
{
[...]
public function getRouteKeyName()
{
return 'slug';
}
}
To use explicit route model binding
Add the following to the boot() method of your RouteServiceProvider:
public function boot()
{
parent::boot();
Route::bind('post', function ($value) {
return App\Post::where('slug', $value)->firstOrFail();
});
}

laravel remove model observer

Is there a way to remove a model observer that is added with
$model->observe(new ObserverObject)
Maybe something like
$model->observers['ObserverObject']->remove()
Thanks
You can check your event name by doing:
dd( $model->getEventDispatcher()->getListeners() );
And remove it using:
$model->getEventDispatcher()->forget($event);
Laravel 5.8
I have an AccountObserver, that observers the creating process on my AccountModel:
class AccountObserver
{
public function creating(AccountModel $account)
{
....
}
}
To disable this, first load the event dispatcher (via any eloquent model), then tell it what to forget. This works for any observer method:
$eventDispatcher = AccountModel::getEventDispatcher();
$eventDispatcher->forget('eloquent.creating: App\Models\AccountModel');
If you want to remember it again:
AccountModel::observe(AccountObserver::class);

Laravel 4 Restored model event not firing

I am trying to get the following code to work with two models using soft deletes. The deleted event fires perfectly and cascade deletes the related models. However when I try to restore the model it fails to called the restored event and the related models are left as deleted.
public static function boot()
{
// make the parent (Eloquent) boot method run
parent::boot();
static::deleted(function ($artwork) {
$artwork->variants()->delete();
});
static::restoring(function ($artwork) {
$artwork->variants()->onlyTrashed()->restore();
});
}
Any ideas?

Resources