2021年3月10日星期三

Laravel attach a callable to onDelete to eloquent model

I have a model that needs to delete images when the model is deleted. The model I will be calling is Seminar.php. This "Seminar" model has got instructors in the model called SeminarInstructor.php. Suppose I called Seminar::find(1)->delete(); to delete the seminar with id = 1-- I would also need this delete off any instructors connected to the model as below:

public function seminarInstructor()      {          return $this->hasMany(SeminarInstructor::class, 'seminar_id', 'id');      }  

I have enabled cascading, so when I call delete, laravel/mysql should automatically resolve it. However, I have a method called public function deleteCloudImage1() in SeminarInstructor.php which handles the deletion logic for the image existing in my table as a url. I could manually call this method before I delete a seminar model, but I want to know if there is any convenient method to handle this in eloquent/laravel. I looked up the code a bit and there seems to be a callable attribute called onDelete in the implementation. Anyone know how to use this, or any other alternative.

https://stackoverflow.com/questions/66575498/laravel-attach-a-callable-to-ondelete-to-eloquent-model March 11, 2021 at 10:07AM

没有评论:

发表评论