我已经通过make:auth生成了用户登录和注册,现在我想更新详细信息

时间:2019-04-15 11:47:03

标签: laravel

我已经通过make:auth生成了用户auth,该用户名没有编辑详细信息字段,现在我想编辑诸如电子邮件和联系人之类的详细信息,而其他用户则保持禁用状态。我试图从RegisterController复制代码,我只是将create方法更改为update方法,如果您有准备好模板以进行编辑的详细信息,请分享给我,我已经搜索了很多现成的模板,但没有找到,我只想模板,它将与生成的模板兼容验证或解决我的问题,因为现在它不更新详细信息 enter image description here

1)查看:edit_profile.blade.php

<form method="POST" action="/profile">

    @csrf

    <div class="form-group row">
        <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

        <div class="col-md-6">
            <input id="name"  disabled type="text" class="form-control" value='{{$user->name}}' name="name"> 
        </div>
    </div>

    <div class="form-group row">
        <label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Student Number') }}</label>

        <div class="col-md-6">
        <input id="username" disabled type="text" class="form-control" name="username" value="{{$user->username}}">
        </div>
    </div>

    <div class="form-group row">
        <label for="age" class="col-md-4 col-form-label text-md-right">{{ __('Age') }}</label>

        <div class="col-md-6">
            <input id="age" disabled type="text" class="form-control" name="age" value="{{$user->age}}"
                required> @if ($errors->has('age'))
            <span class="invalid-feedback" role="alert">
                        <strong>{{ $errors->first('age') }}</strong>
            </span> @endif
        </div>
    </div>

    <div class="form-group row">
        <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

        <div class="col-md-6">
            <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{$user->email}}"
                required> @if ($errors->has('email'))
            <span class="invalid-feedback" role="alert">
                    <strong>{{ $errors->first('email') }}</strong>
                </span> @endif
        </div>
    </div>


    <div class="form-group row">
        <label for="contact" class="col-md-4 col-form-label text-md-right">{{ __('Contact Number') }}</label>

        <div class="col-md-6">
            <input id="contact" type="text" class="form-control{{ $errors->has('contact') ? ' is-invalid' : '' }}" name="contact" value="{{$user->contact}}"
                required> @if ($errors->has('contact'))
            <span class="invalid-feedback" role="alert">
                        <strong>{{ $errors->first('contact') }}</strong>
                    </span> @endif
        </div>
    </div>

    <div class="form-group row mb-0">
        <div class="col-md-6 offset-md-4">
            <button type="submit" class="btn btn-primary">
                {{ __('Update Details') }}
            </button>
        </div>
    </div>
</form>

2)控制器:ProfileController

<?php

namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use Image;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Carbon;

class ProfileController extends Controller
{
    public function profile(){
        return view('pages.profiles.profile', array('user' => Auth::user()) );
    }

    public function update_avatar(Request $request){

        // Handle the user upload of avatar
        if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->crop(300, 300)->save( public_path('/storage/images/avatars/' . $filename ) );

            $user = Auth::user();
            $user->avatar = $filename;
            $user->save();
        }

        return view('pages.profiles.profile', array('user' => Auth::user()) );

    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'email' => 'required|string|email|max:255|unique:users',
            'contact' => 'numeric|digits_between:7,15',
        ]);
    }

    public function edit(){
        return view('pages.profiles.edit_profile', array('user' => Auth::user()) );
    }

    public function update(array $data){
        return User::update([
            'email' => $data['email'],
            'contact' => $data['contact'],
        ]);
    }
}

路线

//User Profile
Route::get('/profile', 'ProfileController@profile');
Route::post('profile', 'ProfileController@update_avatar');
Route::get('/profile/edit', 'ProfileController@edit');
Route::post('profile/edit', 'ProfileController@update');

2 个答案:

答案 0 :(得分:1)

我注意到了一些问题。

  1. 您是ProfileController@update方法,它接受数组,但不会传递数组。
  2. 您不是在经过身份验证的用户上呼叫update

  3. 您要发布到/profile,它会查看您的路线(如果要更新头像而不是用户数据)。

将表单更改为:

<form method="POST" action="/profile/edit">

将您的update方法更改为:

public function update(Request $request)
{
    $data = $this->validate($request, [
        'email'   => 'required|email',
        'contact' => 'required',
    ]);

    auth()->user()->update($data);

    return auth()->user();
}

Documentation for Laravel's Validation

答案 1 :(得分:0)

public function update(Request $request)
{
    //check validation 


    Auth::user()->update($request);

    return true;
}