adideas/laravel-get-relationship-eloquent-model

Laravel 获取 Eloquent 模型关系

dev-master 2021-05-03 14:56 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:31 UTC


README

拥有一个或多个 Eloquent 模型,借助此包,您可以在运行时获取所有其关系及其类型!

有时这确实是必要的,例如对于有与其他实体关联的系统删除操作。例如,您需要删除一个模型并动态地找出还需要删除什么。为此,您不需要知道模型中的方法名称。更不用说根据架构调整名称,而是根据其他东西调整。

此包是为需要了解关系的其他开发者创建的。

Laravel 如何获取所有关系

如何获取所有关系的名称

getRelations()

--

拥有一个或多个 Eloquent 模型,借助此包,您可以在运行时获取所有其关系及其类型。

有时这确实是必要的,例如,对于有与其他实体关联的系统删除操作。例如,您需要删除一个模型并动态地找出还需要删除什么。您不需要知道模型中的方法名称来做这件事。更不用说根据架构调整名称,而是根据其他东西调整。

此包是为需要了解关系的其他开发者创建的。

查看(获取)任何 Eloquent Laravel 模型的所有链接(关系)

安装

使用以下命令通过 composer 安装此包:

composer require adideas/laravel-get-relationship-eloquent-model

请打星标 :)

用法

use App\Http\Controllers\Controller;
use Adideas\RelationFinder\Relations;

class MyController extends Controller
{



    public function show(MyModel $myModel, Relations $relations)
    {
        $relations->where('name', 'myRelationFunction'); // return collect

        dd($relations); // return collect

        Relations::relations($myModel)->where('name', 'myRelationFunction'); // return collect

    }



}

或者

use Adideas\RelationFinder\RelationsFinder;

class MyModel extends Model
{
    use RelationsFinder;

}
use App\Http\Controllers\Controller;

class MyController extends Controller
{



    public function show(MyModel $myModel)
    {
        MyModel::relations()->where('name', 'myRelationFunction'); // return collect
        
        // or

        $myModel->relations()->where('name', 'myRelationFunction'); // return collect
    }



}

配置

如果您想看到所有模型!请安装密钥。

class MyModel extends Model
{
    const WITH_VENDOR_RELATION = true;
    
    // or

    public $withVendorRelation = true;
}