Controller with Model

Controller has following function:

use App\Models\Semester\read\Model as SemesterModel;
...

public function __invoke($request, $response)
{
  $result = SemesterModel::IwantItAll();
  return $response->withJson($result);
}

The model

<?php
namespace App\Models\Semester\read;

use Db\Eloquent\Semester\Model as BaseModel;

class Model extends BaseModel
{
    function IwantItAll() {
        return self::All();
    }
}

Notice that:

return self::All();

is a static function call. This has all to do with the BaseModel.

Db\Eloquent\Semester\Model:

<?php

namespace Db\Eloquent\Semester;

use Db\Eloquent\Semester\Events as Events;
use Db\Eloquent\Base\Model as BaseModel;

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

    protected $fillable = [
        'hash',
        'name',
        ...
        'pduser',
        'pddate',
        'active'
    ];

    protected $events = [
        'deleting' => Events\Deleting::class,
        'deleted'  => Events\Deleted::class,
        'updating' => Events\Updating::class
    ];

    /**
     * Has many seminars
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function seminars()
    {
        return $this->hasMany('Db\Eloquent\Seminar\Model', 'semester', 'id')
            ->where('active', 1);
    }
}

As you can see this is also an extended class, so let's have a look a the parent class.

Db\Eloquent\Base\Model:

namespace Db\Eloquent\Base;

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

class Model extends EloquentModel
{
    use SoftDeletes;

    const CREATED_AT = 'pcdate';
    const UPDATED_AT = 'pmdate';
    const DELETED_AT = 'pddate';

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

    /**
     * load the app container
     * @param $container
     */
    public function withContainer($container) {
        $this->container = $container;
    }

    public function loadPage($start = 0, $limit = 50, $searchArgs = [], $active = 1) {

        $records = $this::all()
            ->setPerPage($limit)
            ->where('active', '=', $active);
        return $records;
    }

    /**
     * Creation User
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function pcuser()
    {
        return $this->hasOne('Db\Eloquent\User\Model', 'id', 'pcuser');
    }

    /**
     * Modification user
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function pmuser()
    {
        return $this->hasOne('Db\Eloquent\User\Model', 'id', 'pmuser');
    }

    /**
     * Deleted user
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function pduser()
    {
        return $this->hasOne('Db\Eloquent\User\Model', 'id', 'pduser');
    }
}

Notice:

use Illuminate\Database\Eloquent\Model as EloquentModel;

at the beginning of the source. This model is an Eloquent model. That means you can use all the
methods from the query builder.

Last update: January 18th at 10:36am