2015-09-05 53 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'); 
    } 
    ... 
} 
相關問題