[英]How to show and edit same blade in laravel?
I have a user and profile table and user_id is the foreign key in the profile table and oneto one relationship. 我有一个用户和个人资料表和user_id是在配置表和一对多一个关系的外键 。 Now I am trying to show the data of user and profile in text field at the profile blade.
现在,我试图在配置文件刀片的文本字段中显示用户和配置文件的数据。 Also, I want to save edit data in using same text field by clicking save change button.
另外,我想通过单击“ 保存更改”按钮在同一文本字段中保存编辑数据。 I can showing the user table data using
value="{{Auth::user()->mobile}}"
but confused to show profile data and how to edit user and profile data. 我可以使用
value="{{Auth::user()->mobile}}"
显示用户表数据,但很困惑以显示配置文件数据以及如何编辑用户和配置文件数据。 How can I edit the all data? 如何编辑所有数据?
Profile Blade 型材刀片
<div class="tab-pane active" id="tab_1_1">
<form role="form" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label class="control-label">Full Name</label>
<input type="text" value="{{Auth::user()->name}}" name="name" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Email</label>
<input type="text" value="{{Auth::user()->email}}" name="email" class="form-control"> </div>
<div class="form-group">
<label class="control-label">Mobile Number</label>
<input type="text" value="{{Auth::user()->mobile}}" name="mobile" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Organization</label>
<input type="text" value="" name="organization" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Department</label>
<input type="text" value="" name="department" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Designation</label>
<input type="text" value="" name="designation" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Address</label>
<textarea class="form-control" rows="3" name="address" value=""></textarea>
</div>
<div class="margiv-top-10">
@if (Auth::user()->id)
<!-- <button type="submit" class="btn btn-primary">Edit</button> -->
<a href="{{route('profileDataUpdate')}}" class="btn default"> Save </a>
@endif
<a href="javascript:;" class="btn default"> Cancel </a>
</div>
</form>
</div>
Controller 调节器
public function profileDataUpdate(Request $request)
{
$this->validate($request,[
'user_id'=> '',
'organization' => '',
'department' => '',
'designation' => '',
'address' => '',
]);
$users = new User();
$users->name = $request->name;
$users->email = $request->email;
$users->mobile = $request->mobile;
$users->save();
$profiles = new Profile();
dd($profiles->user_id = $request->user_id);
$profiles->organization = $request->organization;
$profiles->department = $request->department;
$profiles->designation = $request->designation;
$profiles->address = $request->address;
$profiles->save();
return redirect(route('profile'))->with('successMsg','profile Successfully Updated');
}
First, you need to fetch user information in controller, with that way you show related information easly in your user model 首先,您需要在控制器中获取用户信息,这样才能在用户模型中轻松显示相关信息
user model 用户模型
public function profile(){
return $this->hasOne(Profile::class,"user_id","id");
}
in your controller (we gonna fetch user information) 在您的控制器中 (我们将获取用户信息)
public function showProfile(){
$id = Auth::id();
$user = User::with("profile")->find($id);
return view("profile",compact("user"));
}
view (so in your view, we're able to get profile information with $user variable) 视图 (因此,在您看来,我们可以使用$ user变量获取配置文件信息)
name: {{$user->name}}
organization: {{$user->profile->organization}}
And updating.. You're not updating in your controller, you're trying to create new user and profile. 并正在更新。您没有在控制器中进行更新,而是试图创建新的用户和配置文件。
I assume that profile informations doesn't exist at the first, so we need to use updateOrCreate method 我假设配置文件信息最初不存在,所以我们需要使用updateOrCreate方法
public function profileDataUpdate(Request $request)
{
// validation stuff here
User::where("id", $request->user_id)
->update([
"name" => $request->name,
"email" => $request->email,
"mobile" => $request->mobile
]);
Profile::updateOrCreate(["user_id" => $request->user_id],[
"organization" => $request->organization,
"department" => $request->department,
"designation" => $request->designation,
"address" => $request->address,
]);
return redirect(route('profile'))->with('successMsg','profile Successfully Updated');
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.