Is there a force deleting event? - Laravel 5.2

Asked
Active3 hr before
Viewed126 times

6 Answers

forcedeletingevent
90%

The next step would be to extend the softDeleting trait to fire this event for you automatically.,You can create your own event and fire it yourself. Would be easiest to extend the existing Delete Event and maybe call it Force Deleting.,So to answer your question of if it exists, that answer would be no. Not according to the documentation of Laravel 5.8,What event can I call for that? When calling forceDeleting I get:

Better approach is this:

// GOOD approach
static::deleted(function($document) {
   if (Document::withTrashed() - > where('id', $model - > id) - > exists()) {
      // document is soft deleted
   } else {
      // document is force deleted
   }
});
88%

I'm developing a Laravel web app using Laravel 5.2. My question is very simple... How do I listen to a forceDelete event in order to forceDelete model relations? ,The deleted event should still fire when calling forceDelete(). Inside the deleted() event method, you can check the the forceDeleting protected property via isForceDeleting() to see if you're in a regular delete or a forced delete.,How do I determine if a model uses soft deletes in Laravel 4.2? In the Laravel API I found the function isSoftDeleting(), but apparently that was removed from...,Since both models have SoftDeletes the static::deleted function is called only when the delete() method is called. if I call a forceDelete() method the related model won't be deleted from the database.

Registry table

| id | name | surname | ....
load more v
72%

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.,As we have seen, Eloquent methods like all and get retrieve multiple records from the database. However, these methods don't return a plain PHP array. Instead, an instance of Illuminate\Database\Eloquent\Collection is returned.,You may call the truncate method to delete all of the model's associated database records. The truncate operation will also reset any auto-incrementing IDs on the model's associated table:,Of course, when using Eloquent, we don't only need to retrieve models from the database. We also need to insert new records. Thankfully, Eloquent makes it simple. To insert a new record into the database, you should instantiate a new model instance and set attributes on the model. Then, call the save method on the model instance:

To get started, let's create an Eloquent model. Models typically live in the app\Models directory and extend the Illuminate\Database\Eloquent\Model class. You may use the make:model Artisan command to generate a new model:

php artisan make: model Flight
load more v
65%

I'm developing a Laravel web app using Laravel 5.2. My question is very simple... How do I listen to a forceDelete event in order to forceDelete model relations? ,The deleted event should still fire when calling forceDelete(). Inside the deleted() event method, you can check the the forceDeleting protected property via isForceDeleting() to see if you're in a regular delete or a forced delete.,Since both models have SoftDeletes the static::deleted function is called only when the delete() method is called. if I call a forceDelete() method the related model won't be deleted from the database.,I've been looking around the web and S.O. for a few but all the questions/answers I've found where releted to the delete method, and also in the API documentation I haven't found very much...

Registry table

| id | name | surname | ....

RegistryDetail table

| id | id_registry | ....

I've created for both this boot function:

protected static
function boot() {
   parent::boot();

   static::deleted(function($registry) {
      // Delete registry_detail
      $registry - > registryDetail - > delete();
   });

   static::restored(function($registry) {
      // Restore registry_detail
      $registry - > registrydetail() - > withTrashed() - > restore();
   });
}
load more v
75%

Note: When executing a mass delete statement via Eloquent, the deleting and deleted model events will not be fired for the deleted models. This is because the models are never actually retrieved when executing the delete statement.,When deleting a model, it will set a timestamp on a deleted_at timestamp column in the table for your model, so be sure to create the deleted_at column in your table first. Or in migration you should call softDeletes() method on your blueprint to add the deleted_at timestamp. Example:,You can delete data after writing it to the database. You can either delete a model instance if you have retrieved one, or specify conditions for which records to delete.,To delete a model instance, retrieve it and call the delete() method:

To delete a model instance, retrieve it and call the delete() method:

$user = User::find(1);
$user - > delete();
load more v
40%

I have something similar and still consider it somewhat confusing to say the least.,All this actually does is execute SQL, probably something like "DELETE FROM analytic WHERE id=2". That's it. It doesn't load the models into memory, etc. For these kinds of functions, Laravel is just passing control to an underlying Query Builder query that performs the actions in SQL. That's just something that Eloquent lets you do, it lets you perform Query actions directly on its underlying query object.,Here's another way to do the same thing using Laravel's each() function that it gives you for working on Collections...,@really i don't see why we can fire events from the builder, since we have all the necessary functionality. For now I have extended the builder class, added this function

public static
function boot() {
   static::deleting(function($obj) {});
   static::deleted(function($obj) {});
}
load more v

Other "force-deleting" queries related to "Is there a force deleting event? - Laravel 5.2"