在Laravel关系中以一对一关系的视图显示 [英] Display in VIEW of One to One relation in Laravel Relationship

查看:59
本文介绍了在Laravel关系中以一对一关系的视图显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次遇到麻烦,无法理解正确的Laravel关系方式

I am here again with a trouble understanding the correct way of doing Laravel Relationships

我有这个用户模型

 public function concessionaire()
{
    return $this->hasOne('App\Concessionaire', 'meternum', 'meternum');
}

和特许公司模式

 public function user()
{
    return $this->belongsTO('App\User', 'meternum', 'meternum');
}

但是当我尝试在我的视图中显示它时.特许人数据字段不显示.

But when I try to display it in my view. the concessionaire data fields does not display..

在我的控制器中,我有这个

In my Controller I have this

$dataUser = User::where('usertype', '=', 'concessionaire')
                ->with('concessionaire')
                ->get();
    return view('admin.concessionaire',compact('dataUser'));

在我看来

@foreach($dataUser as $User)
<td>
{{ $User->clark }}
</td>
@endforeach

推荐答案

首先请检查关系函数实现中的外键和本地键是否正确.之后,尝试使数据哑巴

first please check the foreign and local key's are correct in the relation function implementation. after that try dumb the data like

dd($dataUser )

并检查用户模型的关系属性是否实际上包含该关系模型,如果它不为空,则可以访问该属性,例如

and check whether the user model's relations attributes actually contains the relationship model if its not empty you can access the property like

$User->concessionaire->property

如果关系属性显示为空,则可能是在关系实现功能中放置了不正确的本地或外键.

if the relations attributes shows empty then you might have put incorrect local or foreign keys in the relationship implementation function.

您应该遵循

$this->hasOne(Relation::class, 'foreign key in related model', 'local key')

这篇关于在Laravel关系中以一对一关系的视图显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆