[英]How to update value in User Table in laravel 5.1
I have an issue I cannot update my user table .我有一个问题,我无法更新我的用户表。 I don't know where he problem is.Whenever I submit my updated info it cannot updated.
我不知道他的问题出在哪里。每当我提交更新的信息时,它都无法更新。 It Does not show any error.
它不显示任何错误。 But the table is not updated.
但是表没有更新。 Please Help me guys.
请帮助我。 My Controller is
我的控制器是
public function getUpdate() {
$profile = Auth::user();
return view('admin.article.edit')
->with('profile',$profile);
}
public function postUpdate(Request $request ) {
$profile = Auth::user();
$this->validate($request, [
'name' => 'required|max:120',
'username' => 'required|max:80',
'email' => 'required',
'password' => 'required'
]);
// save users table
$profile = Auth::user();
// $user = new App\User;
$profile->name = $request->input('name');
$profile->email = $request->input('email');
$profile->username = $request->input('username');
$profile->password = $request->input('password');
$profile->update();
}
My edit.blade.php page is我的 edit.blade.php 页面是
<?php $active="profile"; ?>
@extends('admin.dashboard')
@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
User Profile
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">User profile</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-10">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Quick Example</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form action="{{ route('update') }}" role="form" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" {{ $errors->has('name') ? 'class=has-error' : '' }} value="{{ Request::old('name') ? Request::old('name') : isset($profile) ? $profile->name : '' }}">
</div>
<div class="form-group">
<label for="username">User Name</label>
<input type="text" class="form-control" name="institute" id="institute" {{ $errors->has('username') ? 'class=has-error' : '' }} value="{{ Request::old('institute') ? Request::old('username') : isset($profile) ? $profile->institute : '' }}">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
<input type="hidden" name="id" value="{{ $profile->id }}">
</div>
</form>
</div>
<!-- /.box -->
</div>
<!--/.col (left) -->
</div>
<!-- /.row -->
</section>
<!-- /.content -->
@endsection
My route file is我的路由文件是
Route::get('/profile/edit', [
'uses' => 'ProfileController@getUpdate',
'as' => 'edit'
]);
Route::post('profile/update', [
'uses' => 'ProfileController@postUpdate',
'as' => 'update'
]);
Try This试试这个
First check you have made fillable all fields you are updating.首先检查您是否已将要更新的所有字段设为可填写。
App/Entites/User.php应用程序/实体/User.php
<?php
protected $fillable = ['name', 'email', 'password','username'];
and then update records然后更新记录
$profile = Auth::user();
$profile->name = $request->name;
$profile->email = $request->email;
$profile->username = $request->institute;
$profile->password = bcrypt($request->password);
$profile->save();
In your route you should write this在你的路线中,你应该写这个
Route::get('/profile/{profile_id}/edit', [
'uses' => 'ProfileController@getUpdate',
'as' => 'edit'
]);
Route::post('profile/update', [
'uses' => 'ProfileController@postUpdate',
'as' => 'update'
]);
And in your controller you should write this在你的控制器中你应该写这个
public function getUpdate() {
$divisions = Division::all();
$districts = District::all();
$dcategories = Dcategory::all();
$profile = Auth::user();
return view('admin.article.edit')
->with('divisions',$divisions)
->with('districts',$districts)
->with('dcategories',$dcategories)
->with('profile',$profile);
}
public function postUpdate(Request $request ) {
$this->validate($request, [
'name' => 'required|max:120',
'fee' => 'required|max:5',
'division_id' => '',
'district_id' => '',
'dcategory_id' => '',
'education' => '',
'institute' => '',
'specialty' => '',
'hospital' => '',
'time' => '',
'phone' => '',
'image' =>''
]);
$doctor = Auth::user();
$doctor->name = $request['name'];
$doctor->division_id = $request['division_id'];
$doctor->district_id = $request['district_id'];
$doctor->dcategory_id = $request['dcategory_id'];
$doctor->institute = $request['institute'];
$doctor->education = $request['education'];
$doctor->specialty = $request['specialty'];
$doctor->hospital = $request['hospital'];
$doctor->phone = $request['phone'];
$doctor->time = $request['time'];
$doctor->fee = $request['fee'];
// $photo = Photo::find($request['id']);
$logo=$request->file('image');
if(!empty($logo)) {
$upload='uploads/logo';
$filename=$logo->getClientOriginalName();
$success=$logo->move($upload,$filename);
$doctor->image = $filename;
}
This might solve your problem.这可能会解决您的问题。
$profile = Auth::user();
creates a new row and $profile->save();
创建一个新行和
$profile->save();
will save the records.将保存记录。
to update the specific row specify that row.eg更新特定行指定该行.eg
$id=$request->input('id');
$profile = User::where(['id'=>$id])->first();
$profile->name = $request->input('name');
$profile->email = $request->input('email');
$profile->username = $request->input('username');
$profile->password = $request->input('password');
$profile->update();
It is not updating because in your Form view there is no username you have institute in the name attribute它没有更新,因为在您的表单视图中,您在 name 属性中没有机构的用户名
name="institute"
Change that to username and try.将其更改为用户名并尝试。 PLUS you don't have fields for password and email.
另外,您没有密码和电子邮件字段。
Assuming you get user from auth().假设您从 auth() 获取用户。 So change
所以改变
$profile = Auth::user();
$profile->name = $request->input('name');
$profile->email = $request->input('email');
$profile->username = $request->input('username');
$profile->password = $request->input('password');
$profile->update();
To至
$profile = Auth::user();
$profile->name = $request->input('name');
$profile->email = $request->input('email');
$profile->username = $request->input('username');
// Hash
$profile->password = \Hash::make($request->input('password'));
// Change to save method
$profile->save();
Try it!试试吧!
//this will load the currently logged in user
$profile = auth()->user();
//if your user db have column 'name' and you wanna update it
$profile->name = 'Jezzabelle';
//save to the db
$profile->save();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.