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.
BelongsTo
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ BelongsTo ;
#[ BelongsTo ( User ::class ) ]
final class Post extends Model
{
use Lift ;
// ...
}
BelongsToMany
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ BelongsToMany ;
#[ BelongsToMany ( Role ::class ) ]
final class User extends Model
{
use Lift ;
// ...
}
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ BelongsToMany ;
#[ BelongsToMany ( User ::class ) ]
final class Role extends Model
{
use Lift ;
// ...
}
HasMany
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ HasMany ;
#[ HasMany ( Post ::class ) ]
final class User extends Model
{
use Lift ;
// ...
}
HasManyThrough
Copy 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 ;
// ...
}
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ HasMany ;
#[ HasMany ( Post ::class ) ]
final class User extends Model
{
use Lift ;
// ...
}
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ BelongsTo ;
#[ BelongsTo ( User ::class ) ]
final class Post extends Model
{
use Lift ;
// ...
}
HasOne
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ HasOne ;
#[ HasOne ( Phone ::class ) ]
final class User extends Model
{
use Lift ;
// ...
}
HasOneThrough
Copy 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 ;
// ...
}
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ HasOne ;
#[ HasOne ( Manufacturer ::class ) ]
final class Computer extends Model
{
use Lift ;
// ...
}
MorphMany/MorphTo
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ MorphMany ;
#[ MorphMany ( Image ::class , 'imageable' ) ]
final class Post extends Model
{
use Lift ;
// ...
}
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ MorphTo ;
#[ MorphTo ( 'imageable' ) ]
final class Image extends Model
{
use Lift ;
// ...
}
MorphOne/MorphTo
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ MorphOne ;
#[ MorphOne ( Image ::class , 'imageable' ) ]
final class User extends Model
{
use Lift ;
// ...
}
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ MorphTo ;
#[ MorphTo ( 'imageable' ) ]
final class Image extends Model
{
use Lift ;
// ...
}
MorphToMany/MorphedByMany
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ MorphToMany ;
#[ MorphToMany ( Tag ::class , 'taggable' ) ]
final class Post extends Model
{
use Lift ;
// ...
}
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ MorphedByMany ;
#[ MorphedByMany ( Post ::class , 'taggable' ) ]
final class Tag extends Model
{
use Lift ;
// ...
}
Customizing the Relationship
All the attributes listed above, except the MorphTo
attribute, accept an additional parameter to customize the relationship name.
Copy 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.
Copy use WendellAdriel \ Lift \ Attributes \ Relations \ BelongsTo ;
#[ BelongsTo ( User ::class , 'author' , 'custom_id' , 'id' ) ]
final class Post extends Model
{
use Lift ;
// ...
}