Reputation: 4614
I am working on a Laravel application that requires user registration and login.
Alter registration, a user should have the possibility to add more info to his/her profile.
For this purpose, I did the following:
In routes/web.php
I have the necessary routes, including update:
Auth::routes();
Route::get('/dashboard', [App\Http\Controllers\Dashboard\DashboardController::class, 'index'])->name('dashboard');
Route::get('/dashboard/profile', [App\Http\Controllers\Dashboard\UserProfileController::class, 'index'])->name('profile');
Route::post('/dashboard/profile/update', [App\Http\Controllers\Dashboard\UserProfileController::class, 'update'])->name('update');
In the newly created Controllers\Dashboard\UserProfileController.php
I have:
namespace App\Http\Controllers\Dashboard; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Auth; use App\Models\UserProfile;
class UserProfileController extends Controller
{
public function index(UserProfile $user)
{
return view('dashboard.userprofile',
array('current_user' => Auth::user())
);
}
public function update(Request $request, $id)
{
$id = Auth::user()->id;
$request->validate([
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
]);
$current_user = User::find($id);
$current_user->first_name = $request->get('first_name');
$current_user->last_name = $request->get('last_name');
$current_user->email = $request->get('email');
$current_user->bio = $request->get('bio');
$current_user->avatar = $request->get('avatar');
$current_user->update();
return redirect('/dashboard/profile')->with('success', 'User data updated successfully');
}
}
In the view file that holds the form (resources\views\dashboard\userprofile.blade.php
) I have:
<form action="{{ route('dashboard/profile/update') }}" enctype='multipart/form-data' method="post" novalidate>
{{csrf_field()}}
<div class="form-group">
<input type="text" id="first_name" name="first_name" placeholder="First name" class="form-control" value="{{$current_user->first_name}}">
@if ($errors->has('first_name'))
<span class="errormsg text-danger">{{ $errors->first('first_name') }}</span>
@endif
</div>
<div class="form-group">
<input type="text" id="last_name" name="last_name" placeholder="Last name" class="form-control" value="{{$current_user->last_name}}">
@if ($errors->has('first_name'))
<span class="errormsg text-danger">{{ $errors->first('last_name') }}</span>
@endif
</div>
<div class="form-group">
<input type="text" id="email" name="email" placeholder="E-mail address" class="form-control" value="{{$current_user->email}}">
@if ($errors->has('email'))
<span class="errormsg text-danger">{{ $errors->first('email') }}</span>
@endif
</div>
<div class="form-group">
<textarea name="bio" id="bio" class="form-control" cols="30" rows="6">{{$current_user->bio}}</textarea>
@if ($errors->has('bio'))
<span class="errormsg text-danger">{{ $errors->first('bio') }}</span>
@endif
</div>
<label for="avatar" class="text-muted">Upload avatar</label>
<div class="form-group d-flex">
<div class="w-75 pr-1">
<input type='file' name='avatar' id="avatar" class="form-control border-0 py-0 pl-0 file-upload-btn">
@if ($errors->has('file'))
<span class="errormsg text-danger">{{ $errors->first('avatar') }}</span>
@endif
</div>
<div class="w-25">
<img class="rounded-circle img-thumbnail avatar-preview" src="{{asset('images/avatars/default.png')}}" alt="{{$current_user->first_name}} {{$current_user->first_name}}">
</div>
</div>
<div class="form-group mb-0">
<input type="submit" name="submit" value='Save' class='btn btn-block btn-primary'>
</div>
</form>
For a reason I was unable to figure out, whenever I am on the dashboard/profile
route (in the browser), Laravel throws this error:
Route [dashboard/profile/update] not defined. (View: Path\to\views\dashboard\userprofile.blade.php)
What am I missing?
Upvotes: 0
Views: 6723
Reputation: 4614
I applied an easy fix, thanks to the info received from the community:
I replaced <form action="{{ route('dashboard/profile/update') }}" enctype='multipart/form-data' method="post" novalidate>
with:
<form action="{{ route('update') }}" enctype='multipart/form-data' method="post" novalidate>
Upvotes: 0
Reputation: 4819
The route()
function accepts a name, not a URL: https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes
So you should have used route('update')
. Though seeing your code, you might not realize the ->name()
method should accept a unique route name. So you should make sure you don't have any other route named 'update'
.
Some people do this: ->name('dashboard.profile.update')
. You can see if you like this convention.
Upvotes: 1