简体   繁体   English

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

[英]Laravel 5.4 update user profile not working

I am trying to update the profile of the user who is logged in. I like to create a function that the user that has logged in when he/she wants to update or edit his/her own profile. 我正在尝试更新已登录用户的个人资料。我想创建一个功能,该功能可以使用户在要更新或编辑自己的个人资料时已登录。 So I got no errors. 所以我没有错误。 But the problem is it wont update and it just refresh my browser but nothing happens. 但是问题是它不会更新,只会刷新我的浏览器,但什么也没发生。

Here is the code on my controller 这是我控制器上的代码

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

Here is the code in my routes: 这是我的路线中的代码:

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

Here is the code in my views: 这是我认为的代码:

edit.blade.php 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

and in fields.blade.php 和在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>

code in my model for validation: 我模型中的代码进行验证:

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

and the UpdateApplicantRequest.php 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;
    }
}

I am looking for help. 我正在寻求帮助。 Thanks in advance. 提前致谢。

There can be several options. 可以有几种选择。 Firstly you need to make sure that right url mapping is created, secondly if the validation is not failing and thirdly if any other middleware is redirecting, for example user is not logged in. 首先,您需要确保创建正确的URL映射,其次,如果验证没有失败,其次,如果其他任何中间件正在重定向,例如用户未登录。

Change the beginning of update method to be 将更新方法的开始更改为

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

If you do not see dd() output then mapping is not right. 如果没有看到dd()输出,则映射不正确。 If you see then validation fails. 如果看到,则验证失败。

Check php artisan route:list and see what middleware is assigned to this route. 检查php artisan route:list并查看分配给该路由的中间件。

Right now it looks like email and password are missing from the fields. 现在看来,这些字段中缺少电子邮件和密码。 Usually you have different validation rules when creating and when updating. 通常,在创建和更新时,您会有不同的验证规则。

Also I advise to have displaying validation errors in your template. 另外,我建议您在模板中显示验证错误。

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

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