如何在Laravel中显示和编辑相同的刀片?

时间:2018-09-05 08:58:21

标签: php laravel-5

我有一个用户和个人资料表,并且 user_id 个人资料表中的外键,并且一对一关系。现在,我试图在个人资料刀片的文本字段中显示用户和个人资料的数据。另外,我想通过单击保存更改按钮在同一文本字段中保存编辑数据。我可以使用value="{{Auth::user()->mobile}}"显示用户表数据,但困惑于显示个人资料数据以及如何编辑用户和个人资料数据。如何编辑所有数据?

个人资料刀片

<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>

控制器

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

1 个答案:

答案 0 :(得分:0)

首先,您需要在控制器中获取用户信息,这样才能轻松显示相关信息 在您的用户模型中

用户模型

public function profile(){
  return $this->hasOne(Profile::class,"user_id","id");
}

在您的控制器中(我们将获取用户信息)

public function showProfile(){
    $id = Auth::id();
    $user = User::with("profile")->find($id);
    return view("profile",compact("user"));
}

视图(因此,在您看来,我们可以使用$ user变量获取配置文件信息)

name: {{$user->name}}
organization: {{$user->profile->organization}}

正在更新。。您不是要在控制器中进行更新,而是要创建新的用户和配置文件。

我认为配置文件信息最初并不存在,因此我们需要使用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');

}