staudenmeir/belongs-to-through

Laravel Eloquent BelongsToThrough 关系

v2.16.1 2024-09-09 12:01 UTC

README

CI Code Coverage PHPStan Latest Stable Version Total Downloads License

HasManyThrough 的逆版本允许使用无限中间模型的 BelongsToThrough 关系。

支持 Laravel 5.0+。

安装

composer require staudenmeir/belongs-to-through:"^2.5"

如果您在 Windows 的 PowerShell 中(例如在 VS Code 中),请使用此命令

composer require staudenmeir/belongs-to-through:"^^^^2.5"

版本

用法

考虑以下 HasManyThrough 关系
Country → 有许多 → User → 有许多 → Post

class Country extends Model
{
    public function posts()
    {
        return $this->hasManyThrough(Post::class, User::class);
    }
}

在您的模型中使用 BelongsToThrough 特性来定义反向关系
Post → 属于 → User → 属于 → Country

class Post extends Model
{
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function country(): \Znck\Eloquent\Relations\BelongsToThrough
    {
        return $this->belongsToThrough(Country::class, User::class);
    }
}

您也可以定义更深层次的关系
Comment → 属于 → Post → 属于 → User → 属于 → Country

将中间模型数组作为第二个参数提供,从相关模型(Country)到父模型(Comment

class Comment extends Model
{
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function country(): \Znck\Eloquent\Relations\BelongsToThrough
    {
        return $this->belongsToThrough(Country::class, [User::class, Post::class]);
    }
}

自定义外键

您可以将自定义外键作为第五个参数指定

class Comment extends Model
{
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function country(): \Znck\Eloquent\Relations\BelongsToThrough
    {
        return $this->belongsToThrough(
            Country::class,
            [User::class, Post::class], 
            foreignKeyLookup: [User::class => 'custom_user_id']
        );
    }
}

自定义本地键

您可以为关系指定自定义本地键

VendorCustomerAddress → 属于 → VendorCustomerVendorCustomerAddress.vendor_customer_id VendorCustomerAddress → 属于 → CustomerAddressVendorCustomerAddress.address_id

您可以通过以下方式从 CustomerAddress 访问 VendorCustomer

class CustomerAddress extends Model
{
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function vendorCustomer(): \Znck\Eloquent\Relations\BelongsToThrough
    {
        return $this->belongsToThrough(
            VendorCustomer::class,
            VendorCustomerAddress::class,
            foreignKeyLookup: [VendorCustomerAddress::class => 'id'],
            localKeyLookup: [VendorCustomerAddress::class => 'address_id'],
        );
    }    
}

表别名

如果您的关联路径包含相同的模型多次,您可以指定一个表别名(Laravel 6+)

class Comment extends Model
{
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function grandparent(): \Znck\Eloquent\Relations\BelongsToThrough
    {
        return $this->belongsToThrough(
            Comment::class,
            Comment::class . ' as alias',
            foreignKeyLookup: [Comment::class => 'parent_id']
        );
    }
}

在您需要别名的模型中使用 HasTableAlias 特性

class Comment extends Model
{
    use \Znck\Eloquent\Traits\HasTableAlias;
}

软删除

默认情况下,软删除的中间模型将从结果中排除。使用 withTrashed() 来包含它们

class Comment extends Model
{
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function country(): \Znck\Eloquent\Relations\BelongsToThrough
    {
        return $this->belongsToThrough(Country::class, [User::class, Post::class])
            ->withTrashed('users.deleted_at');
    }
}

class User extends Model
{
    use SoftDeletes;
}

贡献

请参阅 CONTRIBUTINGCODE OF CONDUCT 以获取详细信息。

鸣谢