Garg Garg - 1 year ago 55
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
. 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

Currently I'm getting -
Undefined index: value

Answer Source

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


Instead of


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


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