I have two models, Topic Model and a Post Model. A Topic can have multiple posts.
public function posts()
{
return $this->hasMany('FPost');
}
Now to get the latest post for a topic I am doing something like the following
public function latestPost()
{
return $this->posts()->where('f_topic_id',"=",$this->id)->take(1);
}
and then I am getting the latest post attributes as such (for a particular topic)
{{$topic->latestPost()->first()['title']}}
Now , I have tried enough , but it seems that this is not working for me
{{$topic->latestPost()->first()->title}}
my question is why can't i get the properties of the post model ?
Update.. The topic model looks like this
class FTopic extends Eloquent {
protected $fillable =['topictitle','topicdescription'];
public function forumCategory()
{
return $this->belongsTo('FCategory');
}
public function user()
{
return $this->belongsTo('User');
}
public function posts()
{
return $this->hasMany('FPost');
}
public function latestPost()
{
return $this->posts()->where('f_topic_id',"=",$this->id)->take(1);
}
public $rules = [
'topictitle' => 'required|min:10',
'topicdescription' => 'required|max:10'
];
public $errors;
public function isValid()
{
$validation = Validator::make($this->attributes, $this->rules);
if($validation-> passes())
return true;
$this->errors = $validation->messages();
return false;
}
protected $table = 'ftopics';
}
Update 2 This is how I am using it. thanks