Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

automatically make created_by and updated_by laravel

<?php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateDatabaseEloquentSoftDeletes;
use IlluminateSupportFacadesAuth;

class Flight extends Model {

    use SoftDeletes;
    use Notifiable;

    /**
     * Table name
     * @var variable
     */
    public $table = 'flight';

     /**
     * For Soft delete
     * @var array
     */
    protected $dates = ['deleted_at'];

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

        static::creating(function ($model) {
            $model->created_by = is_object(Auth::guard(config('app.guards.web'))->user()) ? Auth::guard(config('app.guards.web'))->user()->id : 1;
            $model->updated_by = NULL;
        });

        static::updating(function ($model) {
            $model->updated_by = is_object(Auth::guard(config('app.guards.web'))->user()) ? Auth::guard(config('app.guards.web'))->user()->id : 1;
        });
    }
}
 
PREVIOUS NEXT
Tagged: #automatically #laravel
ADD COMMENT
Topic
Name
6+9 =