[英]Laravel: how to access record by id in view
In my controller I return a view with a ?collection $programs
? 在我的控制器中,我返回一个带有?collection $programs
的视图? from an eloquent query to the view. 从雄辩的查询到视图。
Controller 调节器
$programs = ScheduledProgram::where('registration_start_date', '<=', $today)
return View::make('admin/register_users/show', compact(programs));
I wan to do something like this without it running a new query from the view... 如果没有从视图中运行新查询,我会做这样的事情......
VIEW 视图
{{$program->find(id)}}
I know that $programs
is a dataset that already has the record, but I don't know the way to access the element by ID this way. 我知道$programs
是一个已经有记录的数据集,但我不知道以这种方式通过ID访问元素的方法。
How do I do this? 我该怎么做呢?
(sorry, seems like an obviously searchable question but my search terms aren't comming up with the answer) (抱歉,这似乎是一个明显可搜索的问题,但我的搜索条件并没有得出答案)
in that case you need to make a @foreach in $programs to access the data. 在这种情况下,您需要在$ programs中创建@foreach来访问数据。 Like this: 像这样:
@foreach($programs as $key => $value)
{{$value->id}}
@endforeach
If the return is only one line you can do this: 如果返回只有一行,您可以这样做:
{{$programs[0]->id}}
1.You have to add the method get (converts the "dataset " in a Laravel collection) to iterate the collection. 1.您必须添加方法get(转换Laravel集合中的“数据集”)来迭代集合。
$programs = ScheduledProgram::where('registration_start_date', '<=', $today)
->get()
2.If you want to get a single record: 2.如果您想获得单个记录:
ScheduledProgram::where('registration_start_date', '<=', $today)->where('id', 5)->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.