Laravel 5动态创建雄辩模型 [英] Laravel 5 Dynamically create Eloquent Models

查看:63
本文介绍了Laravel 5动态创建雄辩模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个mysql表名和一个行ID:

Suppose I have an of mysql table names and a row id:

['table_name' => 'things', 'row_id' => 11],
['table_name' => 'stuff', 'row_id' => 5]

每个表都有一个引用表的 Eloquent 模型.如何迭代列表并动态使用模型从 table_name 密钥创建新实例,并根据 row_id 密钥 find 行?

Each of these tables has an Eloquent model referencing the table. How can I iterate the list and dynamically use the Model to create a new instance from the table_name key and find the row based on the row_id key?

我可以通过 Builder 实例获取所需的数据,但实际上我需要构造一个 Model 实例.这是因为我正在通过 Eloquent 模型实现合同,并从该合同中查找特定属性.我无法通过Builder实例获取该属性.

I can get the data I need through a Builder instance but I really need to construct a Model instance. This is because I am implementing a contract through the Eloquent model and looking for a specific attribute from that contract. I can't get the attribute through the Builder instance.

(我正在使用的软件包: https://github.com/jarektkaczyk/revisionable .我正在模型上实现 Revisionable )

(The package that I am using: https://github.com/jarektkaczyk/revisionable. I am implementing Revisionable on the models)

为了澄清一点,这可行:

To clarify a bit, this works:

dd(\App\Models\Thing::find(11)->latestRevision); // returns Revision model

这不是:

// foreach($rows as $row)
$model = new Dynamic([])->setTable($row['table_name']);
dd($model->find($row)); // returns model with correct data
dd($model->find($row['row_id'])->latestRevision); // returns null
// endforeach

如果这还不够清楚,请告诉我.

Please let me know if this is not clear enough.

编辑:

dd($model->find($row)); // returns model with correct data but shows table as `null` as if it isn't being persisted across the request.

此外,这是动态模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sofa\Revisionable\Laravel\RevisionableTrait;
use Sofa\Revisionable\Revisionable;

class Dynamic extends Model implements Revisionable
{
    use RevisionableTrait;

    /**
     * @param $table
     */
    public function __construct($attributes = [])
    {
        parent::__construct($attributes);
    }

    /**
     * Dynamically set a model's table.
     *
     * @param  $table
     * @return void
     */
    public function setTable($table)
    {
        $this->table = $table;

        return $this;
    }
}

推荐答案

首先, latestRevision RevisionableTrait 添加的关系,因此您需要确保 Dynamic 类具有 use RevisionableTrait; 语句.

First, latestRevision is a relationship added by the RevisionableTrait, so you need to make sure that your Dynamic class has the use RevisionableTrait; statement.

接下来,由于 find()方法(以及任何其他检索方法)将返回模型的新实例,因此,找到模型后,您将需要在每个模型上再次重置表.因此,您的代码应类似于:

Next, since the find() method (and any other retrieval methods) return new instances of the models, you will need to reset the table again on every model after you find it. So, your code would look something like:

$model = new Dynamic([]);
$model->setTable($row['table_name']);
$model = $model->find($row['row_id']);
$model->setTable($row['table_name']);
$revision = $model->latestRevision;


假设您遵循Laravel的命名约定,另一种选择是可以从table_name确定 Model 名称,而从一开始就使用正确的 Model Dynamic 模型的模型:


Another option, assuming you follow Laravel's naming conventions, is that you could determine the Model name from the table_name, and just use the correct Model from the start, instead of this Dynamic model:

$modelName = Str::studly(Str::singular($row['table_name']));
$model = $modelName::find($row['row_id']);
$revision = $model->latestRevision;

这篇关于Laravel 5动态创建雄辩模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆