Relationships

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

use WendellAdriel\Virtue\Models\Attributes\Relations\BelongsTo;
use WendellAdriel\Virtue\Models\Concerns\Virtue;

#[BelongsTo(User::class)]
final class Post extends Model
{
    use Virtue;
}

BelongsToMany

use WendellAdriel\Virtue\Models\Attributes\Relations\BelongsToMany;
use WendellAdriel\Virtue\Models\Concerns\Virtue;

#[BelongsToMany(Role::class)]
final class User extends Model
{
    use Virtue;
}

HasMany

HasManyThrough

HasOne

HasOneThrough

MorphMany/MorphTo

MorphOne/MorphTo

MorphToMany/MorphedByMany

Customizing the Relationship

All the attributes listed above, except the MorphTo attribute, accept an additional parameter to customize the relationship name.

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.

Last updated

Was this helpful?