afraid.jpg
afraid.jpg

Reputation: 1175

Eloquent how to do something when delete or update operate on a special model

Most of my db table contain create_user_id and update_user_id

How can l update this two field automatic when l use save(), update(), insert(), createOrUpdate() and etc method.

For example, l execute this script:

$model = Model::find(1);
$model->model_f = 'update';
$model->save();

then this record's model_f updated, and update_user_id updated, too.

l know eloquent can manage update_time automatic and l have use it already. But l want to do something else when update or insert or delete

PS: l have a constant named USERID to remember current user's id

Upvotes: 3

Views: 6298

Answers (2)

ManojKiran
ManojKiran

Reputation: 6351

There is pretty simple way to automatically update the create_user_id and update_user_id

Step1:

Open you app folder and create the new file named as UserStampsTrait.php

Step:2

and paste the following code

<?php
namespace App;
use Illuminate\Support\Facades\Auth;    
trait UserStampsTrait
{
 public static function boot()
  {
    parent::boot();
    // first we tell the model what to do on a creating event
    static::creating(function($modelName='')
    {
       $createdByColumnName = 'create_user_id ';
      $modelName->$createdByColumnName = Auth::id();
    });

    // // then we tell the model what to do on an updating event
    static::updating(function($modelName='')
    {
      $updatedByColumnName = 'update_user_id';
      $modelName->$updatedByColumnName = Auth::id();
    });    
  }
}

Thats it

Step:3

Open you model which needs to updated the corresponding models automatically

for Example it may be Post

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\UserStampsTrait;
class Post extends Model
{
    use UserStampsTrait;
}

Thats it

Upvotes: 3

Mozammil
Mozammil

Reputation: 8750

You could make use of Observers. You can hook to the following events on your Model:

retrieved  
creating
created
updating
updated
saving
saved
deleting
deleted
restoring
restored

Let me give you an example where we are trying to hook into the events emitted by the App/User model. You can change this to match your particular Model later on.

To create an observer, run the following command:

php artisan make:observer UserObserver --model=User

Then you can hook to specific events in your observer.

<?php

namespace App\Observers;

use App\User;

class UserObserver
{

    /**
    * Handle the User "saved" event.
    *
    * @param  \App\User  $user
    * @return void
    */
    public function saved(User $user)
    {
        //
    }


    /**
    * Handle the User "created" event.
    *
    * @param  \App\User  $user
    * @return void
    */
    public function created(User $user)
    {
        //
    }

    /**
    * Handle the User "updated" event.
    *
    * @param  \App\User  $user
    * @return void
    */
    public function updated(User $user)
    {
        //
    }

}

Since, in your particular case, you want to hook into these 3 events, you can define the events above and perform additional operations to your model when those events are called.

Don't forget to register this observer in your AppServiceProvider.

<?php

namespace App\Providers;

use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
    * Bootstrap any application services.
    *
    * @return void
    */
    public function boot()
    {
        User::observe(UserObserver::class);
    }

    /**
    * Register the service provider.
    *
    * @return void
    */
    public function register()
    {
        //
    }
}

Upvotes: 7

Related Questions