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