Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从模型控制器视图(laravel)传递联接查询_Php_Laravel_Laravel 4 - Fatal编程技术网

Php 从模型控制器视图(laravel)传递联接查询

Php 从模型控制器视图(laravel)传递联接查询,php,laravel,laravel-4,Php,Laravel,Laravel 4,我试图通过DB连接进行循环,但出现错误: Undefined variable: ContractorWorkTypePreferences 我在ContractorWorkTypePreference.php模型中创建了联接: public static function workTypesSelected($contractorId){ DB::table('contractor_work_type_preferences') ->join('work_type

我试图通过DB连接进行循环,但出现错误:

Undefined variable: ContractorWorkTypePreferences
我在ContractorWorkTypePreference.php模型中创建了联接:

public static function workTypesSelected($contractorId){
   DB::table('contractor_work_type_preferences')
        ->join('work_types', 'contractor_work_type_preferences.work_type_id', '=', 'work_types.id')
        ->where('contractor_work_type_preferences.contractor_id', '=', $contractorId)
        ->groupBy('work_types.name')
        ->select('work_types.id', 'work_types.name')
        ->get();
} 
然后,我通过以下方式将其传递给Contractorcontroller.php:

public function show()
{

    //// get the contractor
    $arrPageData['contractor'] = Contractor::find($this->userID);   

    // get the work preference of the contractor
    $arrPageData['ContractorWorkTypePreferences'] = ContractorWorkTypePreference::workTypesSelected($this->userID);

    // show the create form and pass the contractor
    return View::make('contractors.show', $arrPageData);


}
最后,我尝试在视图show.blade.php中循环工作类型:

<ul class="tags-list">
            @foreach($ContractorWorkTypePreferences as $work)
            <li><a href="#">{{$work->name}}</a><li>
            @endforeach

        </ul>
    @foreach($ContractorWorkTypePreferences作为$work)
  • @endforeach

任何建议都将不胜感激。TIA.

看起来是打字错误<代码>承包商工作类型首选项和
承包商工作类型首选项
。错误到底发生在哪里?我试过单数和复数。错误发生在以下视图:@foreach($ContractorWorkTypePreferences为$work)我也尝试了@foreach($contractor->ContractorWorkTypePreferences为$work),这导致了错误:为foreach()提供的参数无效首先,该静态方法上缺少
return
。错误是显而易见的,所以只需尝试将该变量重命名为不易出错的变量,然后重试。谢谢@deczo,我很感激。