Awesome
Laplus
Laravel plus+ add presentation for your models
public function present()
{
$this->id();
$this->text('title');
$this->string('password')->cast('hashed')->hidden();
$this->belongsTo(Artist::class)->cascadeOnDelete();
}
Features
1. Auto Migrations
Write your presents in long time:
Laplus will generate your migrations:
2. Auto Fills
You don't need to define $fillable, $cast and $hidden!
// These values will automatically fills:
// protected $fillable = ['id', 'slug'];
// protected $cast = ['password' => 'hashed'];
// protected $hidden = ['password'];
3. IDE Friendly
Laplus automatically generate the model docblock to better understanding codes for IDE:
/**
* @property int $id
* @property string $name
* @property \App\Enums\Gender $gender
* @method BelongsTo<Profile> profile()
* @property Profile $profile
*/
Requirements
- Php 8.2 or higher
- Laravel 11.0
Documents
Installation
1- Install the package with composer:
composer require rapid/laplus
2- Publish configs
Run this command to publish configs to config/laplus.php
php artisan vendor:publish --tag=laplus
3- Convert default User model to presentable model (optional):
- Add
HasPresent
trait:
class User extends Model
{
use HasPresent;
}
- Remove
$fillable
,$hidden
andcasts()
values:
//protected $fillable = ['name', 'email', 'password'];
//protected $hidden = ['password', 'remember_token'];
//protected function casts() { ... }
Laplus will automatically add this values.
- Create
UserPresent
class with the following command:
php artisan make:user-present
Or add below code in User class:
protected function present(Present $present)
{
$present->id();
$present->string('name');
$present->string('email')->unique();
$present->timestamp('email_verified_at')->nullable();
$present->password();
$present->rememberToken();
$present->timestamps();
}
- Move default migration to laplus path:
Find database/migrations/0001_01_01_000000_create_users_table.php
file and move it
into database/migrations/auto_generated
folder (create it if doesn't exists)
Make model & present
You can use this command to create a model and a present:
php artisan make:model+ Name
This command will create app/Models/Name.php
model and app/Presents/NamePresent.php
present.
Make model with inline present
You can use this command to create a model with inline present:
php artisan make:model+ Name --inline
This command will create app/Models/Name.php
model.
Migrations
Generate Migrations
Run this command to automatically found the updates and generate migrations:
php artisan generate+
Regenerate Migrations
This command is same with generate+
, but the difference is clearing the migration folder!
php artisan regenerate+
Warning: This command will remove all your migration files (exclude files starts with
0001_01_01
name)
Generate And Migrate
Following command, run generate+
and migrate
at once:
php artisan migrate+
Label Translator
Present the model labels:
class UserLabelTranslator extends LabelTranslator
{
public function gender(bool $emoji = false)
{
return $this->value?->toString($emoji); // Returns gender name or null
}
}
And use it easily:
<p>{{ $user->gender_label }}</p>
<p>{{ $user->gender_label(emoji: true) }}</p>
Labels always return a string value. If the value is null, it returns
"Undefined"
.
Guide Generator
Guide automatically generate the model docblock using the columns, attributes and relationships:
class User extends Model
{
use HasPresent;
use HasLabels;
protected function present(Present $present)
{
$present->id();
$present->string('name');
}
#[IsRelation]
public function avatars()
{
return $this->hasMany(Avatar::class);
}
public function getFirstName() : string
{
return Str::before($this->name, ' ');
}
}
It generates:
/**
* @Guide
* @property int $id
* @property string $name
* @property Collection<int, Avatar> $avatars
* @property string $name_label
* @property string name_label()
* @property string $first_name
* @EndGuide
*/
class User extends Model
More Document
More document found in Documents section.