0

I'm having an issue passing a variable from a database query to my view. The variable itself passes fine, and I can var_dump it to see the contents of the array. The problem is when I try to call a part of the array, I get an undefined index error.

Controller:

public function displayCustomer()
    {
        $id = $_GET['id'];

$user = DB::table('customers')->where('id', '=',  $id)->get();


                return View::make('single')->withuser($user);

} 

View

<body>
 <?php
    if (Auth::check() != TRUE)
    {
       return Redirect::guest('login')->with('failed', 'Login Failed');
    }
?>


<?php
//var_dump($user);
echo($user['name'])
?>



</body>

So as of right now, I'm able to var_dump the $user array and it works fine, However, when I try to echo out a part of the array I get undefined index, even though I know for a fact 'name' is one of my indexes. I've also tried $user[1] and get got the same error.

var_dump output

array(1) {
    [0]=> object(stdClass)#148 (11) {
        ["id"] => int(1)
        ["name"]=> string(9) "paul rudd"
        ["email"]=> string(18) "[email protected]"
        ["phone"]=> string(10) "6305555555"
        ["address"]=> string(15) "123 fake street"
        ["item"]=> string(13) "6 pcs"
        ["total_price"]=> float(500)
        ["paid_amount"]=> float(400)
        ["balance"]=> float(100)
        ["created_at"]=> string(19) "0000-00-00 00:00:00"
        ["updated_at"]=> string(19) "0000-00-00 00:00:00"
    }
}
2
  • Please paste a copy of the var_dump output for the user. Commented Feb 7, 2014 at 23:28
  • could you show us, even the half part of the result of var_dump or print_r of user? Commented Feb 7, 2014 at 23:28

1 Answer 1

4

Based on your var_dump output try;

<?php echo $user[0]->name; ?>

Your user is an array of objects. In your original query as your specifically picking out one id it might be better to get the first result instead of using a get, like so;

$user = DB::table('customers')->where('id', '=',  $id)->first();

Notice the ->first() then you can simply do;

<?php echo $user->name; ?>
Sign up to request clarification or add additional context in comments.

Comments

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.