Blog Detail

26

Dec
Laravel Helper Package with Model Schema & Observer Methods cover image

arrow_back Laravel Helper Package with Model Schema & Observer Methods

Lucid is an excellent Laravel helper package with model schema, factory definition, & observer methods. Also some Nova tweaks & helper commands for a better DX.

Features

Some of the package features include:

  • define table schemas inside model classes via a schema method
  • define factory definitions inside model classes via a definition method
  • define observer events inside model classes via onX methods
  • quickly install Laravel Nova with a single command
  • Nova resource class naming convention tweaks for a better DX
  • Nova directory structure tweaks for a better DX

Installation

Install Lucid via Composer:

composer require dionsaur84/lucid

Usage

Model Schemas

Lucid allows you to define table schemas directly inside model classes via a schema method:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;

class Post extends Model
{
    public function schema(Blueprint $table)
    {
        $table->id();
        $table->string('title');
        $table->string('slug');
        $table->text('body');
        $table->timestamps();
    }
}

When you run the lucid:migrate command, your database is synced with all the schema methods.

Model Factory Definitions

Lucid allows you to specify factory definitions directly inside model classes via a definition method:

namespace App\Models;

use Dionsaur84\Lucid\Traits\HasDefinition;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasDefinition;

    public function definition()
    {
        return [
            'title' => fake()->sentence(),
            'body' => fake()->paragraph(),
        ];
    }
}

Make sure you use the HasDefinition trait to enable this feature.

When you seed your database via lucid:migrate --seed, the seeders will use what is defined in this method.

Model Observer Events

Lucid allows you to define model observer events directly inside model classes via onX methods:

namespace App\Models;

use App\Jobs\SendNewPostNotifications;
use Dionsaur84\Lucid\Traits\ObservesEvents;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Post extends Model
{
    use ObservesEvents;

    public function onCreating()
    {
        $this->slug = Str::slug($this->title);
    }

    public function onCreated()
    {
        SendNewPostNotifications::dispatch($this);
    }
}

Make sure you are using the ObservesEvents trait in order to enable this feature.

Notice how the methods are named after traditional Laravel observer methods.

With this trait, you can add any of the following methods to your model:

  • onRetrieved
  • onCreating
  • onCreated
  • onUpdating
  • onUpdated
  • onSaving
  • onSaved
  • onDeleting
  • onDeleted
  • onTrashed
  • onForceDeleted
  • onRestoring
  • onRestored
  • onReplicating

For more details and source code please visit Github.

Published at : 26-12-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project