Home > Enterprise >  Laravel 8 error 404 page not found , I wonder where did I do wrong here?
Laravel 8 error 404 page not found , I wonder where did I do wrong here?

Time:10-24

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 >
            <form method="POST" action="/profile/edit/{{$profil->id}}" enctype="multipart/form-data">
                @csrf

                <div >
                    <label  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 App\Http\Controllers;

use App\Models\User;
use App\Models\Profile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

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');
    }
}

CodePudding user response:

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:

public function profile() {
    return $this->hasOne('App\Models\Profile');
}

Then load user profile in update function of controller like:

public function update(Request $request, $id){
    $user = User::with('profile')->findOrFail($id);
    if (is_null($user->profile){ echo 'user don't has profile'; }
    //Update profile from within 

    $user->profile()->updateOrCreate([$request->all()]);

    //NOTE request->all is not safe
}
    

use updateOrCreate() for in case user does not have a profile.

CodePudding user response:

I always name my routes:

Route::post('profile/edit/{id}', [UserController::class, 'update'])->name('user.update')-> middleware('auth')

Then form action looks like this:

<form method="POST" action="{{route('user.update', ['id' =>$profil->id]) }}"

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.

  • Related