Laravel:@foreach不会在其中呈现内容

时间:2017-02-22 14:58:50

标签: php laravel laravel-blade laravel-5.4

我正在创建一个简单的@foreach但由于某种原因它完全被忽略了。我确保数据存在。我在控制器和视图中都进行了var_dumped,并且变量确实存在。

控制器代码:

public function create(Request $request)
    {
        $dawrat = Dawra::all();
        $ainshams = University::where('name', '=', 'عين شمس')->first();

        $faculties = Faculty::where('university_id', '=', $ainshams->id);

        return view('createDawra', ['faculties' => $faculties]);
    }

我的观点:

<div class="btn-group" data-toggle="buttons">
@foreach ( $faculties as $faculty)
    <label class="btn btn-danger " id="collg1">
        <input type="checkbox" autocomplete="off">{{ $faculty->name }}
    </label>
@endforeach
</div>

1 个答案:

答案 0 :(得分:4)

尝试添加->get()

$faculties = Faculty::where('university_id', '=', $ainshams->id)->get()

来自Laravel Eloquent Retrieving Models Docs

  

由于每个Eloquent模型都充当查询构建器,您还可以向查询添加约束,然后使用get方法检索结果