vedajexal/laravel-attributes-relationships

使用属性进行 Laravel eloquent 关联

0.1.0 2021-05-26 16:59 UTC

This package is auto-updated.

Last update: 2024-09-27 00:30:43 UTC


README

使用 PHP 8 属性进行 Laravel eloquent 关联

Build Status codecov

安装

composer require vajexal/laravel-attributes-relationships

该包将自动注册服务提供者

用法

一对一

use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\HasOne;

#[HasOne(Phone::class)]
class User extends Model
{
}

// ...

User::first()->phone;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\BelongsTo;

#[BelongsTo(User::class)]
class Phone extends Model
{
}

// ...

Phone::first()->user;

一对多

use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\HasMany;

#[HasMany(Comment::class)]
class Post extends Model
{
}

// ...

Post::first()->comments;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\BelongsTo;

#[BelongsTo(Post::class)]
class Comment extends Model
{
}

// ...

Comment::first()->post;

多对多

use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\BelongsToMany;

#[BelongsToMany(Role::class)]
class User extends Model
{
}

// ...

User::first()->roles;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\BelongsToMany;

#[BelongsToMany(User::class)]
class Role extends Model
{
}

// ...

Role::first()->users;

通过一个拥有

use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\HasOneThrough;

#[HasOneThrough(Owner::class, Car::class)]
class Mechanic extends Model
{
}

// ...

// If you don't like method name, then you can define relation method
Mechanic::first()->owner;

通过多个拥有

use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\HasManyThrough;

#[HasManyThrough(Post::class, User::class)]
class Country extends Model
{
}

// ...

Country::first()->posts;

一对一(多态)

use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphOne;

#[MorphOne(Image::class, 'imageable')]
class User extends Model
{
}

// ...

User::first()->image;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphTo;

#[MorphTo('imageable')]
class Image extends Model
{
}

// ...

Image::first()->imageable;

一对多(多态)

use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphMany;

#[MorphMany(Image::class, 'imageable')]
class Post extends Model
{
}

// ...

Post::first()->images;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphTo;

#[MorphTo('imageable')]
class Image extends Model
{
}

// ...

Image::first()->imageable;

多对多(多态)

use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphToMany;

#[MorphToMany(Tag::class, 'taggable')]
class Post extends Model
{
}

// ...

Post::first()->tags;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphedByMany;

#[MorphedByMany(Post::class, 'taggable')]
class Tag extends Model
{
}

// ...

Tag::first()->posts;