从模型控制器视图传递连接查询,laravel

时间:2014-05-21 17:24:30

标签: php laravel laravel-4

我正在尝试循环数据库连接,但是我收到了错误:

Undefined variable: ContractorWorkTypePreferences

我在Model 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);


}

最后,我尝试在View,show.blade.php:

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

        </ul>

非常感谢任何建议。 TIA。

0 个答案:

没有答案