0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

7 Comments

Got this: 'Trying to get property of non-object'
Could you give me the output of $free when you var_dump it?
This, which is correct info array(1) { [0]=> object(stdClass)#285 (1) { ["value"]=> string(3) "101" } }
is it big difference between get() and first()? I mean which one is preferable for use?
Yes because you only need one value so there is no need for get() because you don't need multiple records
|
1

Use first() instead of get() and use $free->value, get() return an array of object

1 Comment

Got this: 'Trying to get property of non-object'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.