繁体   English   中英

Laravel 5.4更新用户配置文件不起作用

[英]Laravel 5.4 update user profile not working

我正在尝试更新已登录用户的个人资料。我想创建一个功能,该功能可以使用户在要更新或编辑自己的个人资料时已登录。 所以我没有错误。 但是问题是它不会更新,只会刷新我的浏览器,但什么也没发生。

这是我控制器上的代码

public function edit($id)
    {
        // $applicant = $this->applicantRepository->findWithoutFail($id);

        $applicant = Applicant::where('id',$id)->get()->last();

        if (empty($applicant)) {
            Flash::error('Applicant');

            return redirect(route('applicant.home'));
        }

        return view('applicant-dashboard.edit')->with('applicant', $applicant);
    }

    public function update($id, UpdateApplicantRequest $request)
    {
        $applicant = $this->applicantRepository->findWithoutFail($id);

        if (empty($applicant)) {
            Flash::error('Applicant not found');

            return redirect(route('applicant.home'));
        }

        $input = $request->all();

        $applicant = $this->applicantRepository->update([
            'name' => $input['name'],
            'address' => $input['address'],
            'cellphone_no' => $input['cellphone_no']], $id);

        Flash::success('Profile updated successfully.');

        return redirect(route('applicant.edit'));
    }

这是我的路线中的代码:

Route::get('/edit/{id}', 'HomeController@edit')->name('applicant.edit');
    Route::post('/update', 'HomeController@update')->name('applicant.update');

这是我认为的代码:

edit.blade.php

@extends('applicant-dashboard.layouts.app')

@section('content')
    <section class="content-header">
        <h1>
            Applicant Profile
        </h1>
   </section>
   <div class="content">
       {{-- @include('adminlte-templates::common.errors') --}}
       <div class="box box-primary">
           <div class="box-body">
               <div class="row" style="padding-left: 20px">
                   {!! Form::model($applicant, ['route' => ['applicant.update', $applicant->id], 'method' => 'post']) !!}

                        @include('applicant-dashboard.fields')

                   {!! Form::close() !!}
               </div>
           </div>
       </div>
   </div>
@endsection

和在fields.blade.php

{!! Form::hidden('id', null, ['class' => 'form-control input-sm','required']) !!}

<!-- Name Field -->
<div class="row" style="padding-right: 15px">
<div class="form-group col-sm-4">
    {!! Form::label('name', 'Name:') !!}
    {!! Form::text('name', null, ['class' => 'form-control input-sm','required']) !!}
</div>
</div>

<div class="row" style="padding-right: 15px">
<!-- Address Field -->
<div class="form-group col-sm-4">
    {!! Form::label('address', 'Address:') !!}
    {!! Form::text('address', null, ['class' => 'form-control input-sm','required']) !!}
</div>
</div>

<!-- Cellphone Field -->
<div class="row" style="padding-right: 15px">
<div class="form-group col-sm-4">
    {!! Form::label('cellphone_no', 'Cellphone:') !!}
    {!! Form::text('cellphone_no', null, ['class' => 'form-control input-sm','required']) !!}
</div>
</div>

<div class="row" style="padding-right: 15px">
<!-- Submit Field -->
<div class="form-group col-sm-12">
    {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
    <a href="{!! route('applicant.home') !!}" class="btn btn-default">Cancel</a>
</div>
</div>

我模型中的代码进行验证:

public static $rules = [
        'name' => 'required',
        'email' => 'required|unique:applicants',
        'password' => 'required',
        'password_confirmation' => 'required|same:password'
    ];

UpdateApplicantRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Models\Applicant;

class UpdateApplicantRequest extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return Applicant::$rules;
    }
}

我正在寻求帮助。 提前致谢。

可以有几种选择。 首先,您需要确保创建正确的URL映射,其次,如果验证没有失败,其次,如果其他任何中间件正在重定向,例如用户未登录。

将更新方法的开始更改为

public function update($id, Request $request) {
dd($request->all());

如果没有看到dd()输出,则映射不正确。 如果看到,则验证失败。

检查php artisan route:list并查看分配给该路由的中间件。

现在看来,这些字段中缺少电子邮件和密码。 通常,在创建和更新时,您会有不同的验证规则。

另外,我建议您在模板中显示验证错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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