我正在尝试在登录后更新用户配置文件,它运行良好并且说PROFILE UPDATED SUCCESSFULLY但没有插入数据库表,所以实质上,View不显示更新的表单字段。任何想知道我错在哪里的人都将非常感谢,谢谢。
CONTROLLER更新功能
public function update(Request $request)
{
$rules = [
'name' => 'required',
'email' => 'required',
'phone' => 'required|numeric',
'country' => 'required',
'gender' => 'required',
'birthday' => 'required',
'fb' => 'url',
'twitter' => 'url',
'gp' => 'url',
'instagram' => 'url',
'personal_site' => 'url',
'aboutme' => 'url',
'linkedin' => 'url',
'pinterest' => 'url'
];
$data= $request->all();
$validator = Validator::make($data, $rules);
if($validator->fails()){
return Redirect::back()->withInput()->withErrors($validator);
}
$user = Auth::user();
$user->name = $data['name'];
$user->email = $data['email'];
$user->phone = $data['phone'];
$user->country = $data['country'];
$user->birthday = $data['birthday'];
$user->address = $data['address'];
if($user->save()) {
$profile_id = $user->id;
$profile = Profile::find($profile_id);
if(count($profile) > 0) {
$profile->gender = $data['gender'];
$profile->city = $data['city'];
$profile->state = $data['state'];
$profile->aboutmyself = $data['aboutmyself'];
$profile->fb = $data['fb'];
$profile->twitter = $data['twitter'];
$profile->gp = $data['gp'];
$profile->instagram = $data['instagram'];
$profile->personal_site = $data['personal_site'];
$profile->aboutme = $data['aboutme'];
$profile->linkedin = $data['linkedin'];
$profile->pinterest = $data['pinterest'];
// $profile = $user->profile()->save($profile);
$profile->save();
}
} else {
return redirect()->back()->withInput()->withInfo("Something went wrong. Please, try again");
}
return redirect()->route('profile')->withSuccess("Your Profile Successfully Updated.");
}
我的观点(profile-edit.blade.php)
<div class="form-group row">
{!! Form::model($user, array('route' => 'post.edit.profile', 'method' => 'post', 'class' => 'form-horizontal')) !!}
{!! Form::label('name', "Full Name", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-8">
<div class="row">
<div class="col-md-9">
{!! Form::text('name', null, array('class' => 'form-control', 'placeholder' => 'Your Full Name', 'required' => 'required')) !!}
</div>
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('email', "Email Address", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-6">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">
<i class="material-icons md-18 text-muted">mail</i>
</span>
{!! Form::email('email', null, array('class' => 'form-control', 'placeholder' => '', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('phone', "Phone Number", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-6">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">
<i class="material-icons md-18 text-muted">mail</i>
</span>
{!! Form::text('phone', null, array('class' => 'form-control', 'placeholder' => 'e.g. +8801711223344', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('gender', "Gender", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
{!! Form::select('gender', $gender, array('class' => 'c-select form-control', 'id' => '', 'required' => 'required')) !!}
</div>
</div>
<div class="form-group row">
{!! Form::label('birthday', "Birthday", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('birthday', null, array('class' => 'datepicker form-control', 'placeholder' => '01/28/2016','id' => 'birthday', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('address', "Address", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('address', null, array('class' => 'form-control', 'placeholder' => 'Street No., Area...','id' => 'address', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('city', "City", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('city', $user->profile->city, array('class' => 'form-control', 'placeholder' => 'City', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('state', "State", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('state', $user->profile->state, array('class' => 'form-control', 'placeholder' => 'State', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('country', "Country", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-8">
<div class="row">
<div class="col-md-6">
{!! Form::text('country', null, array('class' => 'form-control', 'placeholder' => 'Country','id' => '')) !!}
</div>
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('aboutmyself', "About Me", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::textarea('aboutmyself', Auth::user()->profile->aboutmyself, array('class' => 'form-control', 'rows' => 4, 'placeholder' => 'About Yourself')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('fb', "Facebook Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('fb', $user->profile->fb, array('class' => 'form-control', 'placeholder' => 'https://facebook.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('twitter', "Twitter Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('twitter', $user->profile->twitter, array('class' => 'form-control', 'placeholder' => 'https://twitter.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('gp', "Google+ Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('gp', $user->profile->gp, array('class' => 'form-control', 'placeholder' => 'https://plus.google.com/+username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('personal_site', "Personal Site", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('personal_site', $user->profile->personal_site, array('class' => 'form-control', 'placeholder' => 'http://www.mywebsite.me')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('instagram', "Instagram Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('instagram', $user->profile->instagram, array('class' => 'form-control', 'placeholder' => 'https://www.instagram.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('linkedin', "LinkedIn Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('linkedin', $user->profile->linkedin, array('class' => 'form-control', 'placeholder' => 'https://www.linkedin.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('pinterest', "Pinterest Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('pinterest', $user->profile->pinterest, array('class' => 'form-control', 'placeholder' => 'https://www.pinterest.com/username')) !!}
</div>
</div>
</div>
<!-- <div class="form-group row">
<label for="password" class="col-sm-3 form-control-label">Change Password</label>
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon3">
<i class="material-icons md-18 text-muted">lock</i>
</span>
<input type="text" class="form-control" placeholder="Enter new password">
</div>
</div>
</div> -->
<div class="form-group row">
<div class="col-sm-8 col-sm-offset-3">
<div class="media">
<div class="media-left">
{!! Form::submit('Save Changes', array('class' => 'btn btn-success')) !!}
</div>
<!-- <div class="media-body media-middle p-l-1">
<label class="c-input c-checkbox">
<input type="checkbox" checked>
<span class="c-indicator"></span> Subscribe to Newsletter
</label>
</div> -->
</div>
</div>
</div>
{!! Form::close() !!}
用户模型(User.php)
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword,EntrustUserTrait {
EntrustUserTrait::can as may;
Authorizable::can insteadof EntrustUserTrait;
}
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function profile(){
return $this->hasOne('App\Profile','user_id','id');
}
public function pending(){
return $this->hasMany('App\PendingTransfers', 'user_id', 'id');
}
public function transaction(){
return $this->hasMany('App\Transaction', 'user_id', 'id');
}
}
ROUTE
Route::group(array('middleware' => 'auth'), function()
{
Route::get('logout', ['as' => 'logout', 'uses' => 'Auth\AuthController@logout']);
Route::get('profile', ['as' => 'profile', 'uses' => 'UsersController@profile']);
Route::get('edit-profile', ['as' => 'edit.profile', 'uses' => 'UsersController@edit']);
Route::post('edit-profile', ['as' => 'post.edit.profile', 'uses' => 'UsersController@update']);
Route::post('edit-photo', ['as' => 'post.edit.photo', 'uses' => 'UsersController@photoUpdate']);
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'Auth\AuthController@dashboard'));
Route::get('change-password', array('as' => 'password.change', 'uses' => 'Auth\AuthController@changePassword'));
Route::post('change-password', array('as' => 'password.doChange', 'uses' => 'Auth\AuthController@doChangePassword'));
更新:感谢提示响应,但是没有解决它,我认为故障可能来自控制器功能此时:
if($user->save()) {
$profile_id = $user->id;
$profile = Profile::find($profile_id);
if(count($profile) > 0) {
$profile->gender = $data['gender'];
$profile->city = $data['city'];
$profile->state = $data['state'];
$profile->aboutmyself = $data['aboutmyself'];
$profile->fb = $data['fb'];
$profile->twitter = $data['twitter'];
$profile->gp = $data['gp'];
$profile->instagram = $data['instagram'];
$profile->personal_site = $data['personal_site'];
$profile->aboutme = $data['aboutme'];
$profile->linkedin = $data['linkedin'];
$profile->pinterest = $data['pinterest'];
// $profile = $user->profile()->save($profile);
$profile->save();
或者可能来自VIEW开始表格路线/变量$ user
{!! Form::model($user, array('route' => 'post.edit.profile', 'method' => 'post', 'class' => 'form-horizontal')) !!}
答案 0 :(得分:2)
在用户模型中设置所有表格字段$fillable
protected $fillable = ['name', 'email', 'password','phone','country','gender'];