【发布时间】: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