[英]Laravel access foreign key table column in blade
到目前为止,我如何使用刀片中的外键访问表列,但是它不起作用。
查询模型/关系
public function client()
{
return $this->belongsTo(Client::class);
}
public function device()
{
return $this->belongsTo(Device::class);
}
客户模型
public function inquiry()
{
return $this->hasMany(Inquiry::class);
}
public function devices()
{
return $this->hasMany(Device::class);
}
设备型号
public function inquiry()
{
return $this->hasMany(Inquiry::class);
}
调节器
public function index()
{
$inquiries = Inquiry::all();
return view('stream.index', compact('inquiries'));
}
视图
@foreach($inquiries as $inquiryKey => $inquiryValue)
<tr>
// stream id is the foreign that table has a column with names ...
<th scope="row">{{ $inquiryValue->stream_id }}</th> // works
<th scope="row">{{ $inquiryValue->stream_id->name }}</th> // does not work -> trying to get property of none object
您无法访问stream_id
属性name
,它只是一个整数,您可以尝试使用$inquiryValue->client->name
或我认为$inquiryValue->client->first()->name
确保让Laravel知道stream_id
是用于您的关系的密钥。
您应该尝试使用以下示例:
更新的答案
调节器
public function index()
{
$inquiries = Inquiry::all();
return view('stream.index', compact('inquiries'));
}
查看文件
@foreach($inquiries as $inquiryKey => $inquiryValue)
<tr>
// stream id is the foreign that table has a column with names ...
<th scope="row">{{ $inquiryValue->client->name }}</th> // works
</tr
@endforeach
如果你的名字的stream_id比我认为这是更好u有一个表名溪流 ,如果u想从查询获得客户端的数据,妳比应该在你的调查表CLIENT_ID列,同为设备,DEVICE_ID列到你的询盘加入表
如何访问它: @foreach($inquiries as $inquiryKey => $inquiryValue) {{$inquiryValue->client->name}} {{$inquiryValue->device->name}} @endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.