0

I want to fetch information of the logged in user stored in the database that is to be shown on his profile page. At the time of registration, he/she is been asked to enter the data and only a few fields are mandatory but in the 'edit profile' page he/she can add more information and then return to his profile page, where all the data should be updated. But the problem here is that only name and email is been fetched.

Controller code:

public function updateProfile(Request $req) {
        $name = Auth::user()->name;
        $user = Admin::where('name', $name)->first();
        if($req->input('admin-name')!= null) {
            $user->name = $req->input('admin-name');

        }

        if($req->input('admin-email')!= null) {
            $user->email = $req->input('admin-email');
        }
        if($req->input('admin-address')!= null) {
            $user->address = $req->input('admin-address');
        }
        if($req->input('admin-mobile')!= null) {
            $user->mobile = $req->input('admin-mobile');
        }
        if($req->input('admin-dob')!= null) {
            $user->dob = $req->input('admin-dob');
        }


        $user->save();


        return redirect('admin-profile')->with('update-response','Profile Updated successfully');
}

View:

<div class="col-sm-6 col-md-8">
                    <h4>
                    {{Auth::user()->name}}</h4>
                    <small><cite title="">{{Auth::user()->address}} <i class="glyphicon glyphicon-map-marker">
                    </i></cite></small>
                    <p>
                        <i class="glyphicon glyphicon-envelope"></i>{{Auth::user()->email}}
                        <br />
                        <i class="glyphicon glyphicon-globe"></i>Contact: {{Auth::user()->mobile}}
                        <br />
                        <i class="glyphicon glyphicon-gift"></i>Born at {{Auth::user()->dob}}</p>    
                </div>
1
  • Can you dd($req); inside your updateProfile() function and make sure all the inputs are sent in the request? Commented Feb 26, 2018 at 16:54

2 Answers 2

0

You can just change the reference to the newly updated object of user to laravel instead of using the cached one, like this:

Auth::setUser($user);

Or you can work on your Auth::user object directly, like this:

...
$user = Auth::user();
$name = $user->name;
...

Thanks but i have to redirect user's data to other view page

return redirect('admin-profile')->with([
    'update-response' => 'Profile Updated successfully', 
    'admin' => $user
]);

Then in your view:

<h4>{{$admin->mobile}}</h4>
3
  • Thanks but i have to redirect user's data to other view page Commented Feb 26, 2018 at 12:56
  • Error:Undefined variable: admin Commented Feb 26, 2018 at 13:04
  • Show us your route for admin-profile and code if you have any in your controller for this route Commented Feb 26, 2018 at 13:08
0

Define an accessor in your User model.

class User extends Model{

    public function getMobileAttribute($value)
    {
        return $value;
    }
}

After this you can access it:

{{ Auth::user()->mobile }}
3
  • Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ')' (View: /opt/lampp/htdocs/laravel/blog/resources/views/admin-profile.blade.php) Commented Feb 26, 2018 at 12:59
  • 1
    Should be Auth::user()->mobile.
    – Tpojka
    Commented Feb 26, 2018 at 13:43
  • Yup sorry :) Auth::user()->mobile Commented Feb 26, 2018 at 14:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.