Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Slillz's avatar

Undefined variable user: view

I'm using laravel 5.3 and I keep getting an error saying undefined variable once I try and log in or click on edit profile. If I manually put "myapp.dev/profit/edit/1" in my browser it works but if I click on the edit profille link I'm only seeing "myapp.dev/profit/edit" in the URL. I've been at this for two days. Any help would be appreciated.

My routes:

 Route::get('/profile/edit/{id}', 'ProfileController@getEdit');
 Route::post('/profile/edit/{id}', 'ProfileController@postEdit');

My controller:

public function getEdit($id){

       $user= User::findOrFail($id);
       return view('profile.edit', compact('user'));
}

My view:

<li><a href="{{ url('/profile/edit', $user->id ) }}">Update profile</a></li>

My form:

 {!! Form::model($user,['method' => 'POST', 'action'=>['ProfileController@postEdit', $user-      >id]]) !!}
0 likes
30 replies
Snapey's avatar

View source in the browser and look for the form tag..

Check the URL. It should end in the ID. Is that working?

Slillz's avatar

Yes when I look at the source for the edit form the action is ending with the ID.

Slillz's avatar

Could the issue be because i;m passing he variable to my profile.edit and not my navigation where the it says the error is at? My profie.edit extends the navigation layout.

Snapey's avatar

What does postEdit() look like?

Slillz's avatar

Currently it's empty. I was working on the getPost() first

Snapey's avatar

sorry i was confused by you posting your form code. This is not relevant then?

So you have a simple link that goes to the edit page?

just check the url generated in the link. You know the get method works because you have already tested it.

Slillz's avatar

Yes the URL that;s generated is myapp.dev/profile/edit. The variable is what's missing. Everything looks correct. Seems like i've tried everything,

MWDeveloper's avatar

change the method in the view and route to patch or put since you are binding the model and want to update.

Snapey's avatar

url second parameter should be an array. try

<li><a href="{{ url('/profile/edit', [ $user->id ]) }}">Update profile</a></li>
Slillz's avatar

@MWDeveloper I tried that put still undefined but shouldn't i be get since I;m just trying to retrieve the form?

Slillz's avatar

Is it possible that the variable wont be made available to the parent template unless I specifically tell it to be like: @extends('master', ['user' => $user]) ?

Slillz's avatar

@Snapey $User is set in my ProfileController as you can see with my initial post.

Snapey's avatar

i would dd user immediately before the url method

Slillz's avatar

@Snapey When I do that I still get the error but if I type in the correct URL(myapp.dev/profile/edit/1) it shows me the array with the users information

Snapey's avatar

still get the error

still get what error?

Slillz's avatar

@Snapey is the $user variable available to the navigation layout since I'm passing it to the profile edit with. The profile/edit does extend that layout though. The error I get is

C:\xampp\htdocs\Myapp\resources\views\layouts\partials\navigation.blade.php)

MWDeveloper's avatar

@Slillz call the route using route instead of url or try this url('/profile/edit/'.$user->id '

use the concatenation

Snapey's avatar

when posting and you have a specific error then it helps to say where in the code, method, line number etc

it should be accessible in the layout but when I asked you to dd the value just before it is used you said the error still occurred which means it's happening before that point?

Slillz's avatar

@Snapey Here is my exact error...

Undefined variable: user (View: C:\xampp\htdocs\Myapp\resources\views\layouts\partials\navigation.blade.php) (View: C:\xampp\htdocs\Myapp\resources\views\layouts\partials\navigation.blade.php) (View: C:\xampp\htdocs\Myapp\resources\views\layouts\partials\navigation.blade.php)

I agree that it must be happening before that point.

Slillz's avatar

Once I started with the form binding is where the issue started.

Slillz's avatar

@MWDeveloper I've tried that but still nothing. As Snapey said I think the issue is before that since the DD gives an error as well.

Slillz's avatar

Now as soon as I sign in I get an error but one I remove that href from the navigation blade the site works fine except for that profile edit.

Snapey's avatar

if you change $user->id for Auth::user()->id

Slillz's avatar

Yea that still didn't work. As you've stated earlier, something else is going on. I notice now I'm getting undefined variable:user my userblock layout. This previously worked fine. Not sure what's going on now. My $user variable isn't being recognized on any view.

vipin93's avatar

in master layout. dont include navigation.blade.php instead of this just @yeild('nav') after this just @extende('navigation) @include('navigation') @stop where u want actually even I don't know why not passing this in my master blade when I Directly include navigation in master but when i use @yeild its worked for me. just try once may be its help

RajanSingh's avatar

First check is $user->id has value or not for this use dd($user->id).

If has a value then use

{{ url('/profile/edit/'.$user->id ) }}

Please or to participate in this conversation.