php Laravel更新仅刷新页面

niknxzdl  于 2023-01-12  发布在  PHP
关注(0)|答案(2)|浏览(267)

我的更新页面无法正常工作,当我提交表单时,它只会刷新页面并向我的URL添加一些内容,例如: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。我已经清除了路由缓存,但它仍然不工作,还尝试使用dd函数,但它没有显示。可能是什么问题?我的型号:

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

控制器功能:

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

    }

请求:

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

查看:

<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::put('/admin/employees/{id}/edit', [EmployeeController::class, 'update'])->name('admin.employees.update');
icomxhvb

icomxhvb1#

表单中缺少method属性,默认属性为GET。添加@method('PUT')是不够的。请按以下方式更改代码:

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

guykilcj2#

我认为您应该在提交表单时使用POST方法。

相关问题