cmxperts/belongs-to-one

属于一个Laravel 5关系类

v1.0 2023-07-25 05:51 UTC

This package is not auto-updated.

Last update: 2024-09-19 21:09:02 UTC


README

Travis CI

基于 属于多个 关系。返回一个模型而不是模型集合。

安装

composer require cmxperts/belongs-to-one

用法

BelongsToOne 关系几乎与标准的 BelongsToMany 相同,除了它返回一个模型而不是模型集合,并在数据库中没有相关模型时返回 null(在这种情况下 BelongsToMany 返回空集合)。在您的模型中包含 cmXperts\Database\Eloquent\Relations\BelongsToOneTrait 并使用 BelongsToOne 关系。

示例

<?php

namespace App\Models;

use App\Models\Group;
Use Illuminate\Database\Eloquent\Model;
use cmXperts\Database\Eloquent\Relations\BelongsToOneTrait;

class Record extends Model
{
    use BelongsToOneTrait;
    
    public function group()
    {
        return $this->belongsToOne(Group::class);
    }
}

然后像使用其他任何关系一样使用它

$record = Record::with('group')->first();

测试

./vendor/bin/phpunit