ankurk91/laravel-eloquent-relationships

为 Laravel PHP 框架添加缺失的 Eloquent 关联。

2.1.0 2024-02-17 05:55 UTC

This package is auto-updated.

Last update: 2024-09-17 07:21:34 UTC


README

Packagist GitHub tag License Downloads tests codecov

此包向 Laravel 的 Eloquent 添加一些缺失的关联。

安装

您可以通过 composer 安装此包。

composer require ankurk91/laravel-eloquent-relationships

使用方法

BelongsToOne

BelongsToOne 关联几乎与标准的 BelongsToMany 相同,只是它返回一个模型而不是模型的集合,并在数据库中没有相关模型时返回 null(在这种情况下 BelongsToMany 返回空的集合)。示例

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ankurk91\Eloquent\HasBelongsToOne;
use Ankurk91\Eloquent\Relations\BelongsToOne;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Restaurant extends Model
{
    use HasBelongsToOne;
    
    /**
     * Each restaurant has only one operator.
     */
    public function operator(): BelongsToOne
    {
        return $this->belongsToOne(User::class)          
            ->wherePivot('is_operator', true);
            //->withDefault();
    }

    /**
     * Get all employees including the operator.
     */
    public function employees(): BelongsToMany
    {
        return $this->belongsToMany(User::class)
            ->withPivot('is_operator');
    }   
}    

现在您可以通过以下方式访问关联

<?php

// eager loading
$restaurant = Restaurant::with('operator')->first();
dump($restaurant->operator);
// lazy loading
$restaurant->load('operator');
// load nested relation
$restaurant->load('operator.profile');
// Perform operations
$restaurant->operator()->update([
  'name'=> 'Taylor'
]);

MorphToOne

MorphToOne 关联几乎与标准的 MorphToMany 相同,只是它返回一个模型而不是模型的集合,并在数据库中没有相关模型时返回 null(在这种情况下 MorphToMany 返回空的集合)。示例

<?php

namespace App\Models;

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

class Image extends Model
{ 
    public function posts(): MorphToMany
    {
        return $this->morphedByMany(Post::class, 'imageable');
    }

    public function videos(): MorphToMany
    {
        return $this->morphedByMany(Video::class, 'imageable');
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ankurk91\Eloquent\HasMorphToOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Ankurk91\Eloquent\Relations\MorphToOne;

class Post extends Model
{
    use HasMorphToOne;

    /**
     * Each post may have one featured image.
     */
    public function featuredImage(): MorphToOne
    {
        return $this->morphToOne(Image::class, 'imageable')
            ->wherePivot('featured', 1);
            //->withDefault();
    }
    
    /**
     * Get all images including the featured.
     */
    public function images(): MorphToMany
    {
        return $this->morphToMany(Image::class, 'imageable')
            ->withPivot('featured');
    }

}

现在您可以通过以下方式访问关联

<?php

// eager loading
$post = Post::with('featuredImage')->first();
dump($post->featuredImage);
// lazy loading
$post->load('featuredImage');

测试

composer test

安全性

如果您发现任何安全问题,请通过电子邮件 pro.ankurk1[at]gmail[dot]com 而不是使用问题跟踪器。

归属

许可

MIT 许可证。