Search
 
SCRIPT & CODE EXAMPLE
 

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;
        });
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: laravel slug 
Php :: range in php 
Php :: carbon now 
Php :: php remove array element 
Php :: join multiple tables in laravel eloquent 
Php :: lodash tester 
Php :: json_encode() in php 
Php :: how to get product id by sku in woocommerce 
Php :: How to write a loop in PHP 
Php :: wordpress escape string 
Php :: how to get all post fields in wordpress 
Php :: Ways to write comments in PHP 
Php :: php cheatsheet 
Php :: php now 
Php :: brew install php 5.6 
Php :: php check if object is empty 
Php :: get class name from object php 
Php :: codeigniter 4 limit query 
Php :: sort array php 
Php :: how to receive json data in php 
Php :: find substring in string php 
Php :: get day by date in php 
Php :: artisan make migration with model 
Php :: laravel drop foreign key 
Php :: php implode keys 
Php :: laravel make model all with resources api 
Php :: php echo variable 
Php :: laravel new line in language file 
Php :: laravel wherin softdelete 
Php :: check if not empty blade engine 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =