lundi 23 avril 2018

Laravel: Undefined variable after a post action

I'm making profile edit page and I had a problem after post action.

Routes file is

Route::get('profile', 'ProfileController@profile')->name('profile');  
Route::post('profile', 'ProfileController@update');

Profile page Controller

public function profile()
    {
        $cities = City::all();
        $current_city = Auth::user()->city;
        $current_city_name = City::cityName($current_city);
        return view('profile.edit', compact('cities', 'current_city', 'current_city_name'));
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'email' => 'required|string|email|max:255',
        ]);
    }

    public function update(Request $request)
    {
        $this->validator($request->all())->validate();
        $user = Auth::user();
        $user->email = $request->input('email');
        $user->city = $request->input('city');
        $user->save();
        $request->session()->flash('message', 'Information successfully changed');
        return view('profile.edit');
    }

And finally blade template code

<select name="city" id="city" class="form-control">
      <option value=""></option>
      @foreach ($cities as $city)
          <option value=""></option>
      @endforeach
</select>

First entrance on profile page is ok. But after form submitting I have an error

Undefined variable: current_city (View: C:\OSPanel\domains\ang\laravel\resources\views\profile\edit.blade.php)



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2JkbHg2
via IFTTT

Aucun commentaire:

Enregistrer un commentaire