php - 从 Laravel 数据库列获取值(value)

标签 php mysql laravel-4

可能很简单,但我有一个表,我想从其中的一列中获取值。它们是两条记录,并且始终是两条记录,我需要第二个 id 的值。这是视觉效果

id     key      value
 1    key_1     Value_1
 2    key_2     value_2

我想获取value_2。这是我的 Controller 中的内容

$free = DB::table('settings')->select('value')->where('key', '=', 'free')->get();
    return View::make('site.cart.order', [
        'cart' => $cart,
        'free' => $free
    ]);

在我看来,我正在尝试这个

@if( $total < $free['value'] )

   // loop
@endif

目前我得到 - undefined index :值

最佳答案

如果我是对的,Laravel 会返回一个标准类,你应该像这样访问它。

$free->value

而不是

$free['value']

但是你现在必须更改它选择第一条记录

$free = DB::table('settings')->select('value')->where('key', '=', 'free')->first();

然后你就可以像这样访问它

$free->value

或者,如果您不想将 get() 更改为first(),您可以像这样访问它,因为 get() 返回一个数组。

$free[0]->value

关于php - 从 Laravel 数据库列获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38633425/

相关文章:

mysql - Spring Boot+Hibernate向mysql插入空值

php - laravel4 无法以管理员身份登录

php - 如何启用 laravel 4 gzip?

php - Laravel4 验证器和可空字段

mysql - MySQL 中的完全外连接而不重复值

php - jQuery完整日历$.ajax调用问题(在页面打开时使用静态事件)

javascript - 如何从 mysqltable 中选择所有数据并将其显示在 html 上

php - 卸载 Laravel 代客

php - array_unique 不适用于 PHP?

javascript - 替换图表中数据表的标签