【问题标题】:Laravel how to run eloquent queries that are value of an array and passed to the view?Laravel如何运行作为数组值并传递给视图的雄辩查询?
【发布时间】:2016-04-17 23:04:02
【问题描述】:

由于一个案例,我需要将多个表 eloquent 查询存储到一个数组中并将它们传递给视图,然后在视图内部我应该处理它们。那么我该怎么做呢,我试过call_user_func()call_user_func_array() bot 没有解决问题并且无法运行。下面是我的雄辩之列。

$my_eloquent_array = array('course' => 'App\Models\Course::all();',
            'user' => 'App\Models\User::whereHas(
                    \'roles\', function($q){
                    $q->where(\'name\',\'course_coordinator\');
                }
                )->get(array(\'id\',DB::raw(\'CONCAT(first_name, " ", last_name) AS full_name\')))'
);

那么现在在循环内的视图中我如何运行那些雄辩的数组值?

$resourceDropdowns = array(
    'curriculum_id'             => array('query' => 'App\Models\ProfessionalDevelopment\Curriculum::all(\'id\',\'name\')', 'label' => 'Curriculum'),
    'classroom_id'              => array('query' => 'Classroom::all(\'id\',\'no\')', 'label' => 'Classroom'),
    'training_coordinator_id'   => array('query' => 'User::whereHas(
                    \'roles\', function($q){
                    $q->where(\'name\',\'course_coordinator\');
                }
                )->get(array(\'id\',DB::raw(\'CONCAT(first_name, " ", last_name) AS full_name\')))', 'label' => 'Training Coordinator'),
    'focal_point_id'            => array('query' => 'User::whereHas(
                    \'roles\', function($q){
                    $q->where(\'name\',\'focal_point\');
                }
                )->get(array(\'id\',DB::raw(\'CONCAT(first_name, " ", last_name) AS full_name\')))', 'label' => 'Focal Point'),
    'master_trainer_id'         => array('query' => 'User::whereHas(
                    \'roles\', function($q){
                    $q->where(\'name\',\'master_trainer\');
                }
                )->get(array(\'id\',DB::raw(\'CONCAT(first_name, " ", last_name) AS full_name\')))', 'label' => 'Master Trainer'),
    'stage_id'                  => array('query' => 'Stage::all(\'id\',\'no\')', 'label' => 'Stage'),
    'stream_id'                 => array('query' => 'Stream::all(\'id\',\'no\')', 'label' => 'Stream'),
    'unit_id'                   => array('query' => 'Unit::all(\'id\',\'no\')', 'label' => 'Unit'),
);;
        if(count($resourceDropdowns) > 0){
            $dropDownFilters = array();
            foreach($resourceDropdowns as $drop_key => $drop_val){
                $dropDownFilters[$drop_key]['query'] = $drop_val['query'];
                $dropDownFilters[$drop_key]['label'] = $drop_val['label'];
            }

        }

但是$drop_val['query'] 没有运行,它只是最终数组中的一个字符串。

【问题讨论】:

  • 是的,它是一个字符串。因为你在它周围加上了''。没有它,您将收到错误消息。所以这不是你的解决方案。
  • 试试我给你建议的方法,如果你遇到任何问题,请告诉我。
  • @RaviHirani 根据我的需要,我根据模型需要在模型中设置了 eloquent 查询数组,然后在用户选择表时在界面中,通过 ajax 请求,我想获得选定的表 eloquent 查询数组并将结果传递给视图。这意味着我不可能在控制器中创建该数组。
  • 然后对其进行编码。 base_64 编码将为您提供帮助。
  • @RaviHirani 内部模型?但在模型中它说expression is not allowed as field default value

标签: php laravel laravel-5 eloquent


【解决方案1】:

你也可以通过这种方式编写相同的代码。

$cources = App\Models\Course::all();
$users =  App\Models\User::whereHas(
                    'roles', function($q){
                    $q->where('name','course_coordinator');
                }
                )->get(array('id',DB::raw('CONCAT(first_name, " ", last_name) AS full_name')));

$my_eloquent_array = array('course' => $cources,
            'user' => $users
); 

return view('VIEW_NAME', ['data' => $my_eloquent_array]);

$drop_val['query'] 替换为eval($drop_val['query'])

注意:- 当您使用 eval() 函数时,请始终将分号(;) 放在每个字符串的末尾。在你的情况下,你必须写

array('query'=>'App\Models\Course::all();');

如果此代码不起作用,请尝试以下方式:-

$arr = array('query'=>base64_encode(App\Models\Course::all()));
$user = base64_decode($arr['query']);

【讨论】:

  • 是的,不是代码的问题,我的问题是如何在视图内运行$users
  • 当我使用call_user_func($users); 时会出现此错误Call to undefined method Illuminate\Database\Query\Builder::all('id','name')() ,而没有call_user_func() 时只需打印雄辩的查询字符串。
  • @jones:请查看我更新的答案。这里 VIEW_NAME 是您的刀片模板,您可以将刀片文件中的数据打印为 {{ $data }}
  • 你不需要调用 call_user_func 函数。我不太明白你为什么需要调用这个函数?
  • 您可以直接在对象中获取查询结果并将该对象传递给您的视图。在您的视图文件中,您只需打印它。
【解决方案2】:

我不确定这是否有效,但您可以尝试一下。我会尝试存储函数并在循环它们时执行它:

// We store the queries with its associated parameters
$queries = [ ["query" => App\Models\Course::all, "params" => null ]];

// We loop over the functions to be executed
foreach($queries as $query){

    // Now, we execute the function
    $query["query"]($query["params"])->get();
}

我不确定此示例是否有效,但我会向您推荐这样的方法。

您必须考虑一个将复杂参数传递给函数的好解决方案。

【讨论】:

  • 您的代码的问题是,在我的情况下,我应该将 App\Models\Course::all 作为字符串存储在数组中,因为我应该在模型中创建该数组。
猜你喜欢
  • 1970-01-01
  • 2018-09-02
  • 2018-01-26
  • 2021-07-23
  • 1970-01-01
  • 2017-11-11
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多