2014-04-19 99 views
0

朋友你好需要一隻手與此查詢,未能使其工作,我是新與laravel,我有3種型號:與laravel動態查詢4

DiscussCategory

class DiscussCategory extends Eloquent { 

protected $table = 'discuss_category'; 
protected $guarded = array('id'); 

public function status() { 
    return $this->belongsTo('Status'); 
} 

public function discuss() { 
    return $this->hasMany('Discuss'); 
} 

}

討論

class Discuss extends Eloquent { 

protected $table = 'discuss'; 
protected $guarded = array('id'); 

public function discussReplies() { 
    return $this->hasMany('DiscussReplies'); 
} 

public function discussCategory() { 
    return $this->belongsTo('DiscussCategory'); 
} 

public function users() { 
    return $this->belongsTo('Users'); 
} 

}

用戶

class Users extends Eloquent { 

protected $table = 'users'; 

protected $guarded = array('id'); 
protected $hidden = array('password'); 

public function getAuthIdentifier() { 
    return $this->getKey(); 
} 

public function getAuthPassword() { 
    return $this->password; 
} 

public function getReminderEmail() { 
    return $this->email; 
} 

public function discussReplies() { 
    return $this->hasOne('DiscussReplies'); 
} 

public function discuss() { 
    return $this->hasMany('Discuss'); 
} 

}

,這我如何努力,以顯示查詢鑑於

    @foreach($data as $category) 
       {{$category->discuss->last()->users->nickname}} 
       @endforeach 

如果你運行一個foreach設法獲取用戶,但我只想要最後一位用戶的姓名發表討論

Trying to get property of non-object 

appreciate a hand, insurance is not much, but I have little experience 

回答

0

在你Discuss模式變革下面的方法:

public function users() { 
    return $this->belongsTo('Users'); 
} 

爲了這個(使用userusers

public function user() { 
    // Assumed one discuss belongs to one user 
    return $this->belongsTo('Users'); // Rename Users to User and use User here 
} 

你也應該爲所有的車型例如,使用用單數名User而不是Users