At the most basic of understanding, I’ve been trying to match the route and the form action. I think that I am doing it right but I wonder why does the error keeps on showing ? I may have missed something anywhere but I just really couldn’t find it. Please help. In a very tight schedule, I need to complete this project by tuesday
P.S : when i submit the form it goes to this address http://127.0.0.1:8000/profile/edit/1 .
Form
<x-layout>
<x-setting heading="Edit Staff Profile">
<div class="flex flex-col">
<form method="POST" action="/profile/edit/{{$profil->id}}" enctype="multipart/form-data">
@csrf
<div class="mb-6">
<label class="block mb-2 uppercase font-bold text-sm text-gray-700" for="images">
Profile photo
</label>
<input type="file" name="images">
</div>
Route
Route::get('profile', [UserController::class, 'index'])->middleware('auth')->name('profile');
Route::get('profile/edit/{id}', [UserController::class, 'show'])->middleware('auth');
Route::post('profile/edit/{id}', [UserController::class, 'update'])->middleware('auth');
UserController
<?php
namespace AppHttpControllers;
use AppModelsUser;
use AppModelsProfile;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
class UserController extends Controller
{
public function index()
{
$id = Auth::user()->id;
$info = User::where('id', $id)->first();
return view('profile', compact('info'));
}
// public function create()
// {
// return view('staffrecord.create');
// }
public function store()
{
$attributes = request()->validate([
'name' => 'required|max:255',
'username' => 'required|min:3|max:255|unique:users,username',
'email' => 'required|email|max:255|unique:users,email',
'password' => 'required|min:7|max:255',
]);
if (auth()->attempt($attributes)) {
return redirect('/')->with('success', 'Your account has been created.');
}
return redirect('/profile')->with('errors', 'Authentication failed.');
}
public function show($id)
{
$profil = User::findOrFail($id);
return view('staffrecord.edit', compact('profil'));
}
public function edit()
{
$id = Auth::user()->id;
$profil = Profile::findOrFail($id);
return view('staffrecord.edit', compact('profil'));
}
public function update(Request $request, $id)
{
$data = Profile::findOrFail($id);
$data->staff_id = $request->staff_id;
$data->name = $request->name;
$data->gender = $request->gender;
$data->address = $request->address;
$data->email = $request->email;
$data->phonenumber = $request->phonenumber;
$data->department = $request->department;
$data->save();
return redirect('/')->route('profile');
}
}
2
Answers
A user may has his logins but they may not have setup their profiles so when you do such request profile find will fail and return to 404 error.
Also to make a note
ALWAYS
use foreign keys in Profile table linking to user id it’s not necessary that a user->id say 1 will have profile->id 1.in User model add this function:
Then load user profile in update function of controller like:
use
updateOrCreate()
for in case user does not have a profile.I always name my routes:
Then form action looks like this:
This way ‘id’ defined in route will be easier to be identified.
Naming routes and using route() may save you some headaches when moving to production.