Laravel 显示连接的客户信息
我有两个表,分别是Laravel 显示连接的客户信息,laravel,laravel-5,Laravel,Laravel 5,我有两个表,分别是user和customer,第一个表有以下字段:(id、姓名、电子邮件、密码) 然后,表customer包含以下字段:(id、name、firstname) 在我的控制器客户中,通过我的函数index(),我必须从我的表user中检索id\u user 问题1:我的两张表之间的关系正确吗 型号客户 protected $fillable = ['name', 'firstname', 'user_id']; public function user() { retu
user
和customer
,第一个表有以下字段:(id、姓名、电子邮件、密码)
然后,表customer
包含以下字段:(id、name、firstname)
在我的控制器客户中,通过我的函数index(),我必须从我的表user
中检索id\u user
问题1:我的两张表之间的关系正确吗
型号客户
protected $fillable = ['name', 'firstname', 'user_id'];
public function user()
{
return $this->belongsTo('App\User');
}
public function customers()
{
return $this->hasOne('App\Customer');
}
型号用户
protected $fillable = ['name', 'firstname', 'user_id'];
public function user()
{
return $this->belongsTo('App\User');
}
public function customers()
{
return $this->hasOne('App\Customer');
}
问题2:如何检索已连接用户的ID
我现在只有这个
public function index()
{
$customers = Customer::orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
问题3:我想了解这两个答案,但我也想了解如何通过我的循环修改我的index.blade.php文件
@foreach($customers as $customer)
<tr>
<td> {{$customer->name}}</td>
<td> {{$customer->firstname}}</td>
@foreach($customers作为$customer)
{{$customer->name}
{{$customer->firstname}
谢谢你的解释 你的模型很好。如果您想为每个
客户
返回用户
关系,您应该这样做:
public function index()
{
$customers = Customer::with('user')->orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
public function index()
{
$customers = Customer::with('user')->orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
{Customer}
|
•id
•name
•firstname
•{user}
|
• id
• name
• email
@foreach($customers as $customer)
<tr>
<td>{{ $customer->user->id }}</td>
<td>{{ $customer->firstname }}</td>
<td>{{ $customer->name }</td>
</tr>
@endforeach
在您看来,您应该:
@foreach($customers as $customer)
<tr>
<td> {{$customer->name}}</td>
<td> {{$customer->firstname}}</td>
<td> {{$customer->user->id}}</td>
@foreach($customers作为$customer)
{{$customer->name}
{{$customer->firstname}
{{$customer->user->id}
更多关于这个
问题1:我的两张表之间的关系正确吗
是的,你们的关系是正确的
说明:
对于customer
表,您指出客户配置文件属于且仅属于一个用户。这是一种反向的一对一实体关系
对于用户
表,您指出一个用户有一个,并且只有一个客户配置文件。这是一种一对一的实体关系
问题2:如何检索已连接用户的ID
通过调用with()
方法,使用Laravel的急切加载来检索客户数据,其中包含关联的用户数据,如下所示:
public function index()
{
$customers = Customer::with('user')->orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
public function index()
{
$customers = Customer::with('user')->orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
{Customer}
|
•id
•name
•firstname
•{user}
|
• id
• name
• email
@foreach($customers as $customer)
<tr>
<td>{{ $customer->user->id }}</td>
<td>{{ $customer->firstname }}</td>
<td>{{ $customer->name }</td>
</tr>
@endforeach
说明:
返回的结果可以可视化如下:
public function index()
{
$customers = Customer::with('user')->orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
public function index()
{
$customers = Customer::with('user')->orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
{Customer}
|
•id
•name
•firstname
•{user}
|
• id
• name
• email
@foreach($customers as $customer)
<tr>
<td>{{ $customer->user->id }}</td>
<td>{{ $customer->firstname }}</td>
<td>{{ $customer->name }</td>
</tr>
@endforeach
最后,您可以在视图中获得包含用户ID的结果,如下所示:
public function index()
{
$customers = Customer::with('user')->orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
public function index()
{
$customers = Customer::with('user')->orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
{Customer}
|
•id
•name
•firstname
•{user}
|
• id
• name
• email
@foreach($customers as $customer)
<tr>
<td>{{ $customer->user->id }}</td>
<td>{{ $customer->firstname }}</td>
<td>{{ $customer->name }</td>
</tr>
@endforeach
@foreach($customers作为$customer)
{{$customer->user->id}
{{$customer->firstname}
{{$customer->name}
@endforeach
有关Laravel实体关系的更多详细信息,请参阅相关的。什么是
customer
表中的外键?@mare96:user\u id