0

I know I am very close, I'm just stuck and can't figure out this last step.

I'm trying to echo out some information in my blade template, but I'm getting the following error: Undefined property: Illuminate\Database\Eloquent\Collection::$Moniker

Here is my controller, which returns the $data variable to the view:

$user = User::where('id', $request)->get();
$data = array(
    'images'  => $images,
    'user'   => $user
);
return view('a_profile')->with('data', $data);  

Here is my view:

<title>{{$data['user']->Moniker}} | My Company | Baltimore, MD</title>

How do I get the Moniker which is a column in my users table to echo out, in this case? Thank you! :)

6
  • Are user and moniker have relationships? Commented Mar 17, 2016 at 1:51
  • @aldrin27 Sorry, come again? Commented Mar 17, 2016 at 1:52
  • Can you print_r($user) ? Commented Mar 17, 2016 at 1:55
  • @aldrin27 It is kind of a mess and it has sensitive data in it, but there is a relationship, as Moniker is a column in my users table. Commented Mar 17, 2016 at 2:04
  • 1
    Okay I understand. If you want to get the columns in that use foreach. For example: @foreach($data['user'] as $key => $val) {{$val->Moniker}} @endforeach in your blade. Commented Mar 17, 2016 at 2:06

1 Answer 1

1

using get() method will return you records in array into array format like this,

your query,

$user = User::where('id', $request)->get();

result would be this

array(
   array(
     'id'  =>1,
     'name'=>'xyz' 
   )
)

So, in your view, instead of looping, you can access the column doing this,

<title>{{$data['user'][0]->Moniker}} | My Company | Baltimore, MD</title>

So convert your query into this first(), because you want to return single record,

$user = User::where('id', $request)->first();

this will return the result into single array format

array(
  'id'  =>1,
  'name'=>'xyz' 
)

and you can access into your view, simple doing this

<title>{{$data['user']->Moniker}} | My Company | Baltimore, MD</title>
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.