简体   繁体   中英

Laravel update only refreshes a page

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=brekke.lola%40example.org&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" class="form-label">Photo</label>
                <input type="file" name="image_path" class="form-control-file" >
                @error('image_path')
                    <div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
                @enderror

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


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


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



                <label for="position_id" class="form-label">Position</label>
                <select class="form-control" 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 class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
                @enderror


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


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


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



                <div class="mt-4 text-center">
                    <button type="submit" class="btn btn-secondary">Submit</button>
                </div>
            </form>

Route:

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

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">

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM