wendelladriel/laravel-lift
Experimental Laravel package that supercharges Eloquent models with typed public properties matching your schema, powered by PHP 8 attributes. Add validation rules and other metadata directly on models and access them via handy methods, using Eloquent events for easy drop-in use.
With Lift, you can configure all of your Model relationships using Attributes. It works the same way when defining them with methods, so all of them accept the same parameters as the methods.
use WendellAdriel\Lift\Attributes\Relations\BelongsTo;
#[BelongsTo(User::class)]
final class Post extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\BelongsToMany;
#[BelongsToMany(User::class)]
final class Role extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\HasMany;
use WendellAdriel\Lift\Attributes\Relations\HasManyThrough;
#[HasMany(User::class)]
#[HasManyThrough(Post::class, User::class)]
final class Country extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\HasMany;
#[HasMany(Post::class)]
final class User extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\BelongsTo;
#[BelongsTo(User::class)]
final class Post extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\HasOne;
#[HasOne(Phone::class)]
final class User extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\HasOne;
use WendellAdriel\Lift\Attributes\Relations\HasOneThrough;
#[HasOneThrough(Manufacturer::class, Computer::class)]
#[HasOne(Computer::class)]
final class Seller extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\HasOne;
#[HasOne(Manufacturer::class)]
final class Computer extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\MorphMany;
#[MorphMany(Image::class, 'imageable')]
final class Post extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\MorphTo;
#[MorphTo('imageable')]
final class Image extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\MorphOne;
#[MorphOne(Image::class, 'imageable')]
final class User extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\MorphTo;
#[MorphTo('imageable')]
final class Image extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\MorphToMany;
#[MorphToMany(Tag::class, 'taggable')]
final class Post extends Model
{
use Lift;
// ...
}
use WendellAdriel\Lift\Attributes\Relations\MorphedByMany;
#[MorphedByMany(Post::class, 'taggable')]
final class Tag extends Model
{
use Lift;
// ...
}
All the attributes listed above, except the MorphTo attribute, accept an additional parameter to customize the relationship name.
use WendellAdriel\Lift\Attributes\Relations\BelongsTo;
#[BelongsTo(User::class, 'author')]
final class Post extends Model
{
use Lift;
// ...
}
$post->author; // Will return the User model
After the name parameter, you can pass the same parameters as you would do when defining the relationship using methods, for example, to customize the foreign key.
use WendellAdriel\Lift\Attributes\Relations\BelongsTo;
#[BelongsTo(User::class, 'author', 'custom_id', 'id')]
final class Post extends Model
{
use Lift;
// ...
}
For the BelongsToMany relationship you can also customize the pivot with the pivotModel and pivotColumns parameters:
use WendellAdriel\Lift\Attributes\Relations\BelongsToMany;
#[BelongsToMany(User::class, pivotModel: OrganizationUser::class, pivotColumns: ['is_owner'])]
class Organization extends Model
{
use Lift;
#[PrimaryKey]
public int $id;
#[Fillable]
public string $name;
}
How can I help you explore Laravel packages today?