【发布时间】:2018-04-02 10:45:52
【问题描述】:
Route::get('/', function (){
$contacts = Contact::all();
//dd($contacts);
return view('welcome')->with('contacts',$contacts);
});
从我通过 $cotacts 到视图的路线
@foreach($contacts as $contact)
<li> {{$contact->name}}
<small>by {{$contact->phoneNumber->number}}</small>
</li>
@endforeach
但是当我要访问phoneNumber的号码时,这是我的课:
public function phoneNumber()
{
return $this->hasOne(PhoneNumber::class);
}
我收到错误试图获取非对象的属性 ???
当我这样说的时候:
<li> {{$contact->name}}
<small>by {{$contact->phoneNumber}}</small>
</li>
我得到:
科尔顿·基尔贝克四世{"id":20,"contact_id":1,"number":"+1-936-288-3493","created_at":"2018-04-01 20:14:16","updated_at":"2018-04-01 20:14:16"}
Shaun Streich DVM by
博士。 Ruthe Thiel III 作者
Laverne Mertz {"id":11,"contact_id":4,"number":"(769) 844-4643 x59321","created_at":"2018-04-01 20:14:16","updated_at":"2018-04-01 20:14:16"}
等等一些名字没有数字。
【问题讨论】:
标签: php laravel-5 table-relationships