【发布时间】:2013-08-19 11:05:34
【问题描述】:
在我之前的问题中,我必须执行原始 SQL 查询。
我已经在我的控制器中声明了一个公共函数:
public function deaths()
{
$getdeaths = DB::statement('SELECT * FROM player_deaths ORDER BY time DESC LIMIT 10');
return View::make('main.latestdeaths')->with('getdeaths', $getdeaths);
}
在检索数据时,我想显示玩家姓名,因此在 foreach 的视图中,尝试运行 SQL 查询。
@foreach($getdeaths as $getdeath)
<tr>
<? $name = DB::select( DB::raw('SELECT name FROM players WHERE id = '$getdeath->player_id'') ) ?>
<td>{{ $getdeath->player_id }}</td>
<td></td>
<td></td>
</tr>
@endforeach
当我尝试回显 $name 时,变量未定义。
我可以用其他方式解析它吗?
【问题讨论】:
-
我最近在一个陌生的代码中看到了这个。为什么有人想从视图中查询?这很丑陋并且违反了 MVC 约定。请避免做这样的事情或习惯于开发结构化的 . . . .关于 Laravel 我不能说太多,但考虑到其他 MVC 框架,您可能对常见且遵循约定的模型关系感兴趣。聪明点。拆分逻辑。