Home > Back-end >  Laravel update only refreshes a page
Laravel update only refreshes a page

Time:01-11

My update page isn't working, when I'm submitting a form it only refreshes a page and adds some stuff to my URL, for example: http://127.0.0.1:8000/admin/employees/102/edit?_method=PUT&_token=mj3lrHqoYm1wiLwWiNQ8OX0kNRjmW4QVRuLpgZxY&image_path=&name=Libbie Ebert&phone_number=380324244497&[email protected]&position_id=7&payment=186799&head=Asia Frami&recruitment_date=2022-01-10. I already cleared route cache and its still not working, and also tried to use dd function but it's not showing up. What can be wrong? My model:

class Employee extends Model
{
    use HasFactory;

    protected $casts = [
        'recruitment_date' => 'datetime:d-m-y',
     ];

    protected $fillable= ['name', 'position_id', 'phone_number',
    'recruitment_date', 'email', 'head',
    'image_path', 'payment', 'admin_created_id', 'admin_updated_id'];

    public function position()
    {
        return $this->belongsTo(Position::class,  'position_id');
    }
}

Controller function:

    public function update(StoreEmployeeRequest $request, $id){
        $input = $request->validated();

        $employee = Employee::findOrFail($id);

        $employee->update([
            'name' => $request->input('name'),
            'position_id' => $request->input('position_id'),
            'phone_number' => ' ' . $request->input('phone_number'),
            'recruitment_date' => $request->input('recruitment_date'),
            'email' => $request->input('email'),
            'head' => $request->input('head'),
            'image_path' => $input['image_path'],
            'payment' => number_format($request->input('payment')),
            'admin_updated_id' => auth()->id(),
        ]);

        return to_route('admin.employees.index');

    }

Request:

public function rules()
    {
        return [
            'name' => 'required|min:2|max:255',
            'recruitment_date' => 'required|date',
            'phone_number' => 'required',
            'email' => 'required|email',
            'payment' => 'required|numeric',
            'position_id' => 'required',
            'image_path' => 'image|mimes:jpg,png|max:5000|dimensions:min_width=300,min_height=300'
        ];
    }

View:

            <form action="{{ route('admin.employees.update', ['id' => $employee->id]) }}" enctype="multipart/form-data">
                @method('PUT')
                @csrf
                <label for="image_path" >Photo</label>
                <input type="file" name="image_path"  >
                @error('image_path')
                    <div  role="alert"><strong>{{ $message }}</strong></div>
                @enderror

                <label for="name" >Name</label>
                <input type="text" name="name"  value="{{ $employee->name }}" placeholder="Name is...">
                @error('name')
                    <div  role="alert"><strong>{{ $message }}</strong></div>
                @enderror


                <label for="phone_number" >Phone</label>
                <input type="text" name="phone_number"  value="{{ old('phone_number', preg_replace('/[^0-9]/', '', $employee->phone_number)) }}" placeholder="380...">
                @error('phone_number')
                    <div  role="alert"><strong>{{ $message }}</strong></div>
                @enderror


                <label for="email" >Email</label>
                <input type="email" name="email"  value="{{ old('email', $employee->email) }}" placeholder="Email is...">
                @error('email')
                    <div  role="alert"><strong>{{ $message }}</strong></div>
                @enderror



                <label for="position_id" >Position</label>
                <select  name="position_id" aria-label=".form-select-lg example">
                    @foreach ($positions as $position)
                        <option value="{{ $position->id }}" {{$employee->position_id == $position->id  ? 'selected' : ''}}>{{ $position->name }}</option>
                    @endforeach
                </select>
                @error('position_id')
                    <div  role="alert"><strong>{{ $message }}</strong></div>
                @enderror


                <label for="payment" >Salary, $</label>
                <input type="text" name="payment"  value="{{ old('payment', preg_replace('/[^0-9]/', '', $employee->payment)) }}" placeholder="Salary is...">
                @error('payment')
                    <div  role="alert"><strong>{{ $message }}</strong></div>
                @enderror


                <label for="head" >Head</label>
                <input type="text" id="head" name="head"  value="{{ old('head', $employee->head) }}" placeholder="Head is...">
                @error('head')
                    <div  role="alert"><strong>{{ $message }}</strong></div>
                @enderror


                <label for="recruitment_date" >Date</label>
                <input type="datetime-local" name="recruitment_date" value="{{ old('recruitment_date', $employee->recruitment_date) }}" >
                @error('recruitment_date')
                    <div  role="alert"><strong>{{ $message }}</strong></div>
                @enderror



                <div >
                    <button type="submit" >Submit</button>
                </div>
            </form>

Route:

Route::put('/admin/employees/{id}/edit', [EmployeeController::class, 'update'])->name('admin.employees.update');

CodePudding user response:

You are missing the method attribute in your form, and the default one is GET. Adding @method('PUT') is not enough. Change your code like this:

<form
    action="{{ route('admin.employees.update', ['id' => $employee->id]) }}"
    method="POST"
    enctype="multipart/form-data">

CodePudding user response:

i think you should you POST method while submitting the form.

  • Related