Laravel对口才模型数组的动态查询 [英] Laravel dynamic queries on array of Eloquent models

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

问题描述

我当前正在尝试创建一个函数,该函数在数组中定义的模型上调用不同的scopeQueries,例如scopeByLocation()scopeByPublished().我已经通过[this link] [1]了解了基础知识.但是,当尝试访问在相应模型中定义的定制查询范围时,出现以下错误:"Call to undefined method Illuminate\Database\Query\Builder::ForLocation($location)->get()".

I am currently trying to make a function that calls different scopeQueries such as scopeByLocation() or scopeByPublished() on models defined in an array. I've got the basics working through [this link][1]. However, when trying to access custom made query scopes that are defined in the corresponding model, I get the following error: "Call to undefined method Illuminate\Database\Query\Builder::ForLocation($location)->get()".

我要实现的是一种方法,该方法循环遍历模型数组中的每个模型并检索&在模型上调用正确的scopeQuery,如下所示:

What I want to achieve is a single method which loops through every model in the array of models and retrieves & calls the right scopeQuery on the model, something like this:

$modelElements = $model::{$queryScope}();

例如$model = 'Modules\News\Models\Article'

$queryScope是模型本身中定义的queryScope.例如. scopeForLocation($location).

And $queryScope is a defined queryScope in the model itself. E.g. scopeForLocation($location).

我已经测试了$queryScope = 'all'并且得到了很好的结果,但是当我尝试访问存在于例如Location模型中的自定义queryScope($queryScope = 'ForLocation($location)->get')时,出现以下错误:"Call to undefined method Illuminate\Database\Query\Builder::ForLocation($location)->get()"

I've tested $queryScope = 'all' and I get a result just fine, however when I try to access a custom queryScope ($queryScope = 'ForLocation($location)->get') that exists within for example the Location model, I get the following error: "Call to undefined method Illuminate\Database\Query\Builder::ForLocation($location)->get()".

这一切都发生在一个foreach循环中,在该循环中,将调用我的models-array中的每个模型,然后在该模型上调用相应的queryScope.

So this all happens in a foreach-loop where every model in my models-array gets called and then the corresponding queryScope gets called on the model.

为什么$queryScope = 'all'方法对我的动态模型有效,但是其他范围会引发错误?我真的希望有人可以帮助我朝着正确的方向发展.

Why does the $queryScope = 'all' method works on my dynamic models, but other scopes throw an error? I really hope someone could help me get into the right direction with this issue.

预先感谢

J.母鹿.

推荐答案

好的,我终于通过以下方式解决了该问题:

Okay, I've finally solved it the following way:

//array of models
public function models()
{
    return [
        'Modules\Website\Models\Article', 
         ...
        ];
}

//function that retrieves all elements for a model
public function getAllElementsForModel($model, $param)
{
    //instantiate model
    $model = new $model;

    //call queryScope
    //'queryScope' could be any queryScope that is defined within your model(s),
    //the parameters are needed for the associated queryScope
    $query = call_user_func_array([$model, 'queryScope'], [$param1, $param2]);

    $result = $query->get();

    //do stuff with your $result
}


//retrieves all 
public function all($param)
{
    //loop through the array of models
    foreach($this->models() as $model){

        $this->getAllElementsForModel($model, $param);
        //do stuff here...
    }
}

分享很贴心!

这篇关于Laravel对口才模型数组的动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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