Minkz singh
Minkz singh

Reputation: 97

Laravel Observer Create Working but Delete Not Working

I am Trying to Using Observer for Deleting With Relationship But Problem Is When i DD in Created Function Its Working Fine But When i DD In Deleted Function It Shows Nothing (POSTMAN) Means Neither Working Nor Error With Same Everything

Here Is Api:

$api->post('store','App\Http\Controllers\CustomerController@store');
$api->delete('delete/{id}','App\Http\Controllers\CustomerController@destroy');

Here Is Observer file made by artisan

namespace App\Observers;

use App\Customer;

class CustomerObserver
{
    public function created(Customer $customer)
    {
        dd($customer);  
    }

    public function deleted(Customer $customer)
    {
        dd($customer); 
    }
}

Here is Customer Controller

class CustomerController extends Controller
{
    public function store(Request $request)
    {
        return Customer::store($request->person);
    }

    public function destroy($id)
    {
       $delete = Customer::where('person_id',$id);
       $delete->delete();
    }
}

Here Is Customer Model File.

class Customer extends Model
{
    //Relationship Start From Here
    public function person()
    {
        return $this->belongsTo(Person::class);
    }

    //End Here

    public static function store($request)
    {
        //Call to Person Model And Save User
        $user = Person::store($request);

        //Create object of Customer Model
        $customer = new Customer();
        $customer->fill($request['customers']);
        $customer->person()->associate($user)->save();

        //return customer 
        return $customer;
    }
}

Upvotes: 6

Views: 15524

Answers (5)

Deepak Kumar
Deepak Kumar

Reputation: 1

Laravel observers are used to monitor and respond to specific events that occur in your application's Eloquent models. When it comes to using observers with soft deletes in Laravel, there are a couple of things to keep in mind.

1- Import the Illuminate\Database\Eloquent\SoftDeletes trait: Before using observers with soft deletes, make sure that your model is using the SoftDeletes trait provided by Laravel. This trait adds the necessary methods and attributes to enable soft delete functionality.

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class YourModel extends Model
{
    use SoftDeletes;

    // ...
}

2- Register the observer: To use an observer, you need to register it with your model. This can be done in the boot method of your model or in the booted method of a service provider.

use App\Observers\YourModelObserver;

class YourModel extends Model
{
    use SoftDeletes;

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

        YourModel::observe(YourModelObserver::class);
    }

    // ...
}

3- Observer implementation: In your observer class, you can define methods to handle the specific events you want to observe. For soft deletes, you can use the deleting and deleted events.

namespace App\Observers;

use App\Models\YourModel;

class YourModelObserver
{
    public function deleting(YourModel $model)
    {
        // This method is called before the model is deleted (soft or hard delete)
    }

    public function deleted(YourModel $model)
    {
        // This method is called after the model is deleted (soft or hard delete)
    }
}

Make sure you have implemented the above steps correctly. If you're still facing issues with your observer not working with soft deletes, please provide more specific details or code examples that can help identify the problem.

Upvotes: 0

MUHAMMAD USMAN
MUHAMMAD USMAN

Reputation: 326

Only these Methods Works

$customer=Customer::where('id',$id)->first();
   if($customer){
          $customer->delete();
      }

Or 

    $customer=Customer::find($id)->delete();

Upvotes: 1

Cheng Shi
Cheng Shi

Reputation: 210

I know this might be a late reply and not sure if you are still looking for the answer. I think the issue is about how you delete your Customer model.

When you do something like

$delete = Customer::where('person_id',$id);
$delete->delete();

You are executing a mass delete statement. As stated in laravel document, mass deletes will not fire any model events for the models that are deleted This is the reason your deleted observer event didn't fire.

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

Now look at how you create a Customer. You create a model one at a time. Therefore your created observer does get run.

//Create object of Customer Model
$customer = new Customer();
$customer->fill($request['customers']);
$customer->person()->associate($user)->save();

To solve your problem, the easiest way is to retrieve all the models and delete one by one, so that you can trigger the event.

foreach (Customer::where('person_id',$id)->get() as $delete) {
   $delete->delete();
}

Upvotes: 12

javielrezende
javielrezende

Reputation: 21

I was also having this problem, however I was calling the delete method from the repository, which prevents the Observer's deleted event from being triggered. When I used the delete from the mode it worked normally.

Upvotes: 1

Jignesh Joisar
Jignesh Joisar

Reputation: 15115

can you do all things

1 add line in Customer::observe(CustomerObserver::class); in CustomerServiceProvider in boot method

  1. add CustomerServiceProvider in app.php file in provider array

  2. composer dump-autoload

  3. php artisan config:cache

Upvotes: 2

Related Questions