如何通过用户 ID laravel 5.4 从单行获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42580005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:27:46  来源:igfitidea点击:

how to get data from single row by user id laravel 5.4

laravellaravel-5.4

提问by Jamal Ahmad

I am new in laravel, i want to get single record of user by user idand display in Input value. please guide me

我是laravel的新手,我想通过用户ID获取用户的单个记录并显示在输入值中。请指导我

Controller

控制器

public function profile(){

    $userdata = User::all();

    return view('pages/edit-profile')->with('userdata',$userdata);
}

Route

路线

Route::get('profile', 'PageController@profile_c');

Form View

表单视图

  <form class="form-horizontal" role="form" action="/edit_profile" method="post">
                    {{  csrf_field() }}
 <div class="form-group">
    <label class="col-lg-3 control-label">First name:</label>
      <div class="col-lg-8">
        <input class="form-control" name="firstName" id="firstName" type="text" value="Jane">
      </div>
 </div>
 <div class="form-group">
   <label class="col-lg-3 control-label">Last name:</label>
     <div class="col-lg-8">
       <input class="form-control" name="lastName" id="lastName" type="text" value="Bishop">
     </div>
 </div>
 <div class="form-group">
  <label class="col-lg-3 control-label">Display name:</label>
    <div class="col-lg-8">
       <input class="form-control" name="displayName" id="displayName" type="text" value="Jane Bishop">
    </div>
 </div>
  <div class="form-group">
    <label class="col-lg-3 control-label">Email:</label>
     <div class="col-lg-8">
      <input class="form-control" name="email" id="email" disabled type="email" value="[email protected]">
     </div>
  </div>
 </form>

回答by EddyTheDove

To get the user by their id

通过用户的 id 获取用户

$user = User::find(id);
return view('pages/edit-profile', compact('user));

If you know something else from the user that is unique like the user email

如果您知道用户的其他独特信息,例如用户电子邮件

$user = User::where('email', $email)->first();
return view('pages/edit-profile', compact('user));

In the form, if you are using blade

在表格中,如果您使用刀片

<input name="email" value={{ $user->email }} />

And more importantly, how to use ELoquent: https://laravel.com/docs/5.4/eloquent

更重要的是,如何使用 ELoquent:https://laravel.com/docs/5.4/eloquent