rapid / laplus
Laravel Plus+
Requires
- laravel/framework: ^11.1|^12.0
Requires (Dev)
- orchestra/testbench: ^10.0
README
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(); }
What's Changed In V4
- Command names changed
- New development & production deploy tools
- Removed slug and file columns
- Supports renames and changes a column at the same time
- Supports package resources
- Added travels (read more...)
- Added validation generators (read more...)
Features
1. Migration Generating
Define the structure of your model: columns, indexes, data types, enums, relationships, or even extensibility with traits.
class Blog extends Model { use HasPresent; use HasSlug; // Will extend the 'slug' column public function present(Present $present) { $present->id(); $present->string('title'); $present->belongsTo(Category::class); $present->enum('status', Status::class); $present->yield(); $present->timestamps(); } }
Don't get involved in migrations! Because Laplus has taken responsibility for all migrations, and it builds all migrations by reading your presentations 😎
2. Auto Fills
No need to redefine $fillable, $cast and $hidden! Defining it in the present specifies everything. So Laplus will autofill these variables. 😉
// These values will automatically fills: // protected $fillable = ['id', 'slug']; // protected $cast = ['password' => 'hashed']; // protected $hidden = ['password'];
3. IDE Friendly
When all the columns, their types, and their relationships are known, why shouldn't the IDE show them to us?
Laplus introduces all of this to the IDE by generating a neat (and out-of-model) document. 📝
/** * @property int $id * @property string $name * @property \App\Enums\Gender $gender * @method BelongsTo<Profile> profile() * @property Profile $profile */
4. Travels
Once a version of the project has been deployed, it is almost impossible to change the database.
Travels allow you to make changes to the table records as well as change the table structure. ✈️
return new class extends Travel { public string|array $on = User::class; public string|array $whenAdded = 'full_name'; public string|array $prepareNullable = 'full_name'; public function up(): void { User::all()->each(function (User $user) { $user->update([ 'full_name' => $user->first_name . ' ' . $user->last_name, ]); }); } };
In the above example, we are going to remove first_name
and last_name
from the users table
and replace them with full_name
.
The above class is responsible for generating the full_name
value using the previous
data before deleting first_name
and last_name
.
Requirements
- Php 8.2 or higher
- Laravel 11.0
Documents
- Installation
- Configuration
- Present
- Dev Utils
- Deploy
- Label
- Guide
- Travel
- Package Development
- Validation Generator
Installation
1- Install the package with composer:
composer require rapid/laplus
2- Publish configs (Optional)
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.
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/deploy
folder (create it if doesn't exists)
Development Utils
To be able to update your tables during development, you can use the following command:
php artisan dev:migrate --guide
Deployment
When deploying the project to production, you can create the final migrations with the following command:
php artisan deploy:migrate
For greater project safety, you can follow the scenario we have written on the page below.
Make model
You can use this command to create a model and a present:
php artisan make:model-laplus Name
This command will create app/Models/Name.php
with inline presentation.
Make model with separately present
You can use this command to create a model with present class:
php artisan make:model-laplus --present Name
This command will create app/Models/Name.php
model and app/Presents/NamePresent.php
present.
Migrations
Generate Migrations
Run this command to automatically found the updates and generate migrations:
php artisan dev:migration
Update Database
Following command, run dev:migration
and migrate
at once:
php artisan dev: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.