26
DecLucid is an excellent Laravel helper package with model schema, factory definition, & observer methods. Also some Nova tweaks & helper commands for a better DX.
Some of the package features include:
Install Lucid via Composer:
composer require dionsaur84/lucid
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.
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.
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:
For more details and source code please visit Github.
Published at : 26-12-2022
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 project