2015-09-05 109 views
1

我试图通过相关模型循环访问我的用户表调用AdminNotes在我的刀片视图中使用foreach。但是,我收到以下错误。有任何想法吗?Laravel 5.1:试图在关系foreach循环中获取非对象的属性

试图获得非对象的属性(查看:/home/vagrant/Code/sass_l5/resources/views/users/edit.blade.php)

在HandleExceptions->的HandleError('8 ','尝试获取非对象的属性','/ home/vagrant/Code/sass_l5/storage/framework/views/5da731c9bf1a02452259c329d3c273e0','226',array('__ path'=>'/ home/vagrant/Code ('Factory'),'app'=> object(Application),'errors'=> object(ViewErrorBag),'__data'=> array('__ env'=> object用户'=>对象(用户)),'obLevel'=>'1','__env'=>对象(Factory),'app'=>对象(应用程序),'errors'=> object(ViewErrorBag) user'=> object(User),'note'=> true))in 5da731c9bf1a02452259c329 d3c273e0线226

我已经建立了这样的关系:

用户模型:

... 
class User extends Model 
{ 
    ... 
    public function adminNotes() 
    { 
     return $this->hasOne('sass\AdminNote'); 
    } 
    ... 
} 

AdminNote型号:

... 
class AdminNote extends Model { 
    ... 
    public function user() 
    { 
    return $this->belongsTo('sass\User'); 
    } 
    ... 
} 

edit.blade.php

... 
@if (count($user->adminNotes) >= 1) 
    @foreach ($user->adminNotes as $note) 
    {{ $note->created_at }} 
    {{ $note->notes }} 
    @endforeach 
@else 
... 

DD($用户> adminNotes):

AdminNote {#274 ▼ 


#connection: null 
    #table: null 
    #primaryKey: "id" 
    #perPage: 15 
    +incrementing: true 
    +timestamps: true 
    #attributes: array:5 [▼ 
    "id" => 25 
    "user_id" => 1 
    "notes" => "Test note" 
    "created_by" => 1 
    "created_at" => "2014-01-01 00:00:00" 
    ] 
    #original: array:5 [▼ 
    "id" => 25 
    "user_id" => 1 
    "notes" => "Test note" 
    "created_by" => 1 
    "created_at" => "2014-01-01 00:00:00" 
    ] 
    #relations: [] 
    #hidden: [] 
    #visible: [] 
    #appends: [] 
    #fillable: [] 
    #guarded: array:1 [▶] 
    #dates: [] 
    #dateFormat: null 
    #casts: [] 
    #touches: [] 
    #observables: [] 
    #with: [] 
    #morphClass: null 
    +exists: true 
    +wasRecentlyCreated: false 
} 

DD($注)

true 
+0

用户可以有多个管理员说明吗? –

+0

是的,它可以有多个。 – RobDiablo

+0

男人,我只是回答我自己的问题,不是吗?感谢帮助指出了这一点。 Ughhh。 – RobDiablo

回答

0

感谢罗斯威尔逊指出,我的人际关系的建立是为了只有一个音符,其中在他们需要被设置有许多笔记。

... 
class User extends Model 
{ 
    ... 
    public function adminNotes() 
    { 
    return $this->hasMany('sass\AdminNote'); 
    } 
    ... 
} 
相关问题