Laravel 4 查询内部 View

标签 laravel laravel-4

在上一个问题中,我必须执行原始 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/

相关文章:

mysql - laravel 在插入之前检查记录是否存在,这是一种有效的方法

php - Laravel 中数据库的复选框值

php - CRUD Laravel 4如何链接销毁?

php - Laravel通过smtp服务器发送邮件报错503 5.5.2

php - Laravel token 不匹配异常

php - 在进行 artisan 迁移时,Laravel Mysql项目中的连接被拒绝

php - 使用 phpunit DB::table()->get() 测试 Laravel 4.2 返回数组而不是对象

mysql - Laravel 分页错误——调用未定义的方法 stdClass::links()

php - SSL 证书错误 : self signed certificate in certificate chain in using Twilio on my Laravel Website

mysql - 使用数据透视表的 Laravel Eloquent 多对多关系