File size: 1,016 Bytes
560ee81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;

class BaseModel extends Model
{
    use HasFactory;
    const LIMIT_PAGE = 10;
    public static function boot()
    {
        parent::boot();

        self::creating(function ($model) {
            if (Auth::user()) {
                if (!$model->created_by && Schema::hasColumn($model->getTable(), 'created_by')) {
                    $model->created_by = Auth::user()->id;
                }

                if (!$model->updated_by && Schema::hasColumn($model->getTable(), 'updated_by')) {
                    $model->updated_by = Auth::user()->id;
                }
            }
        });

        self::saving(function ($model) {
            if (Auth::user() && Schema::hasColumn($model->getTable(), 'updated_by')) {
                $model->updated_by = Auth::user()->id;
            }
        });
    }
}