Base Model (not required)

In the previous chapter a base model was declared:

<?php
namespace Db\Eloquent\Models\Cars;

use Db\Eloquent\Models\Cars\Events as Events;
use Db\Eloquent\Models\Base\Model as BaseModel;

class Model extends BaseModel
{
    protected $table = 'cars';
    protected $primaryKey = 'id';

This model is placed in a folder named Base, named Model.php.

In this model we arrange all those things that count for every other model which extends the base model.

<?php

namespace Db\Eloquent\Models\Base;

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

class Model extends EloquentModel
{
    use SoftDeletes;

    const CREATED_AT = 'cdate';
    const UPDATED_AT = 'mdate';
    const DELETED_AT = 'ddate';

    /**
     * The number of models to return for pagination.
     *
     * @var int
     */
    protected $perPage = 25;

    /**
     * Creation User
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function cuser_info()
    {
        return $this->hasOne(\Db\Eloquent\Models\User\profiles\Tiny::class, 'id', 'cuser');
    }

    /**
     * Modification user
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function muser_info()
    {
        return $this->hasOne(\Db\Eloquent\Models\User\profiles\Tiny::class, 'id', 'muser');
    }

    /**
     * Deleted user
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function duser()
    {
        return $this->hasOne(\Db\Eloquent\Models\User\profiles\Tiny::class, 'id', 'duser');
    }
}

As you can see, you could arrange the soft deletes, which use de ddate and duser fields to deactivate a record without hard deleting it.

Within the events you can fill the duser field and the active field during the Deleting event.

Last update: March 14th at 12:41pm