在上一个问题中,我必须执行原始 SQL 查询。
我已经在我的 Controller 中声明了一个公共(public)函数:
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 View 内尝试运行 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
当我尝试 echo $name 时,该变量未定义。
我可以用其他方式解析它吗?
最佳答案
给你:
$getDeaths = DB::table('player_deaths')->orderBy('time','desc')->take(10)->get();
return View::make('main.latestdeaths')->with('getDeaths', $getDeaths);
这将为您提供按时间排序的所有玩家死亡的对象。
关于Laravel 4 查询内部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18283515/