0

I have a notifications table in database and also have a users table.In notifications table I have user_id,notific_user_id,post_id.I can print_r $user variable which is in Controller but can to access the $user->first_name.

This is the Controller:

public function test()
{
    $result=Notifications::where('user_id',Auth::user()->id)->get();
    echo '<pre>';


    foreach ($result as $result){
        $user=User::where('id',$result->notific_user_id)->first()->get();
        print_r($user);
    }
}

And this is some of the output

 Illuminate\Database\Eloquent\Collection Object
 (
[items:protected] => Array
    (
        [0] => App\User Object
            (
                [connection:protected] => 
                [table:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 100000000
                        [created_at] => 2016-04-28 19:25:01
                        [updated_at] => 2016-05-10 05:03:22
                        [user_name] => 1
                        [first_name] => Tuhin
                        [last_name] => Hossain
                        [nick_name] => 
                        [email] => [email protected]
                        [password] => $2y$10$XtUWwJwVe2qf86CSUN6AkOkQlCWYq8/b3TrvBt2xuMlh4wTzRgxbG
                        [date_of_birth] => 2016-04-30
                        [gender] => 1
                        [remember_token] => a1U4V4rgJogw3hzXE9jJ8kHqtVrR5FkrMlvLEaRsC6qtmgX3LAbB3MkrZAgz
                    )

                [original:protected] => Array
                    (
                        [id] => 100000000
                        [created_at] => 2016-04-28 19:25:01
                        [updated_at] => 2016-05-10 05:03:22
                        [user_name] => 1
                        [first_name] => Tuhin
                        [last_name] => Hossain
                        [nick_name] => 
                        [email] => [email protected]
                        [password] => $2y$10$XtUWwJwVe2qf86CSUN6AkOkQlCWYq8/b3TrvBt2xuMlh4wTzRgxbG
                        [date_of_birth] => 2016-04-30
                        [gender] => 1
                        [remember_token] => a1U4V4rgJogw3hzXE9jJ8kHqtVrR5FkrMlvLEaRsC6qtmgX3LAbB3MkrZAgz
                    )

                [relations:protected] => Array
                    (
                    )

Now how can I access the first_name, last_name etc? I tried print_r($user->first_name); but it did not work. Please help me.

0

1 Answer 1

1

You get a collection (list of objects), since you use the ->get() method for your Eloquent query builder

simply remove the ->get() and you will get the first object matching the query

Your code should look like this:

$user=User::where('id',$result->notific_user_id)->first();
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.