Garg Garg - 4 months ago 10
PHP Question

Getting value from database column Laravel

May be is simple but I have one table where I want to take value from one column. They are two records and always will be two records and I need value from second id.. Here is visual

id key value
1 key_1 Value_1
2 key_2 value_2


I want to take
value_2
. Here is what I have in my controller

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


And in my view I'm trying this

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

// loop
@endif


Currently I'm getting -
Undefined index: value

Answer

If I'm right Laravel returns a standard class there and you should access it like this.

$free->value

Instead of

$free['value']

But you have to change now it selects the first record

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

Then you can access it like

$free->value

Or if you don't want to change get() to first() you can access it like this because get() returns an array.

$free[0]->value
Comments