Slillz
Slillz

Reputation: 165

Laravel undefined variable in view

I'm new to laravel. Using version 5.3 and tried to search but don't see what I'm doing wrong. I keep getting an "Undefined variable: user" in my view. I'm also doing form model binding. Model binding works properly when manually entering URL. Just can't click on link to bring up edit view.

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]]) !!}

Upvotes: 3

Views: 2311

Answers (4)

linuxartisan
linuxartisan

Reputation: 2426

In my opinion, the statement in your view

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

is causing the issue. You may confirm this by looking at the URL in the address bar after clicking on the Update profile link.

Try changing it to this

<li><a href="{{ action('ProfileController@getEdit', $user->id ) }}">Update profile</a></li>

Upvotes: 0

Muhsin VeeVees
Muhsin VeeVees

Reputation: 190

Please edit your getEdit method

public function getEdit($id){
    $user= User::findOrFail($id);
    dd($user);
    return view('profile.edit', compact('user'));
}

check if there is any data user variable.

Upvotes: 0

Sujal Patel
Sujal Patel

Reputation: 2532

Try this for pass data array in view

public function getEdit($id){
    $data['user'] = User::findOrFail($id);
    return view('profile.edit')->withdata($data);
}

in view page try to print $data

{{ print_r($data) }}

Upvotes: 0

Joe1992
Joe1992

Reputation: 468

public function getEdit($id){

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

public function postEdit($id){

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

Upvotes: 1

Related Questions