Models

This section will explain how the database handling works within this Slim framework fork.

The database handling is origined from the scripts in the Models folder. These scripts make a connection to a (Laravel) Eloquent model in de /database/Eloquent folder. Laravel Eloquent is loaded through a Capsule manager class, that can be found in folder /container/singleton/db.

Link to Laravel Eloquent

Slim Model ('/app/Models/User')

Sample:

<?php

namespace app\Models;

use Db\Eloquent\User as UserModel;

class User
{
    public function getFirstRecord()
    {
        return UserModel::first();
    }

    public function setNewName($newName = null)
    {
        if (is_string($newName)) {
           $record = $this->getFirstRecord();

           if ($record !== null) {
               $record->Name = $newName;
               $record->save();
           }
        }   
    }

    public function insertNewRecord(Int $incr)
    {
        $fields = [
            'Hash' => 'abc' . $incr,
            'Name' => 'User ' . $incr,
            'Surname' => 'Surname ' . $incr,
            'Email' => 'Email@user.' . $incr,
            'ExtraData' => '[]',
            'Password' => md5($incr),
            'Active' => 1,
        ];

        UserModel::create($fields);

        return $this;
    }

    public function deleteFirst()
    {
        $u = $this->getFirstRecord();
        $u->delete();
    }

    public function All()
    {
        $result = UserModel::All();

        return $result;
    }

    public function firstOrFail()
    {
        try {
            UserModel::where('Id', '=', 9999)->firstOrFail();
        } catch (\Exception $e) {
            d($e);
        }
    }
}

Eloquent Model ('/database/Eloquent/User.php')

Sample:

<?php
namespace Db\Eloquent;

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

use Db\Eloquent\Observables as Events;

class User extends Model
{
    use SoftDeletes;

    const CREATED_AT = 'Created_at';
    const UPDATED_AT = 'Updated_at';
    const DELETED_AT = 'Deleted_at';

    protected $table = 'users';
    protected $primaryKey = 'Id';

    protected $fillable = [
        'Hash',
        'Name',
        'Surname',
        'Email',
        'ExtraData',
        'Password',
        'Active'
    ];

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

Since Eloquent models are query builders, you should review all of the methods available on the query builder. You may use any of these methods in your Eloquent queries.

Events

As you can see in the sample above there is the following relating to events

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

This relates to the event handling in the '/database/Eloquent/Observables/User'. There you should have a Deleting.php and a Deleted.php. Notice the starting capital !!!.

Sample of the 'Deleting.php':

<?php

namespace Db\Eloquent\Observables\User;

use Db\Eloquent\User;

class Deleting {

    public function __construct(User $User) {
        $User->Active = 0;
        $User->Updated_by = 999;
        $User->update();
    }

}

At first it looks that nothing is happening here, but what it does is fills some fields, and updates the record, before deleting it, but it is not actually deleting it, because in the Eloquent model we have defined:

use SoftDeletes;

which is filling the 'Deleted_at' field with a date and activates with that the soft deletion of the record. The event trigger also sets the 'Active' field (has to be defined) to '0', which is easier to check, when all the active records have a value of '1' by default.

But as you can see, you can influence the contents of the record that you want to soft delete, before it is softly deleted. But you can also use the trigger to do other stuff like logging something in another table.

Last update: Tue, 13 Sep 2022 14:32:15