I am creating category model where it has parent of category model itself.. i did the relations in model and its giving me output in json format. When i do
{{ $category->parent }}
it gives me :
{"id":1,"name":"test","slug":"test","parent_id":0,"is_active":0,"created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00"}
and if i try
{{ $category->parent()->name }}
it gives error ofcourse.
any suggestions on how to fetch parent row object so that $category->parent()->name
would work.
also in which scenario do we have to define table name in model as its working fine if i dont too.
=======================category model====================
class Category extends Eloquent {
protected $fillable = array('name', 'slug', 'parent_id', 'is_active');
public static $rules = array(
'name' => 'required|min:3',
'slug' => 'required',
);
public function parent(){
return $this->belongsTo('Category', 'parent_id');
}
public function children() {
return $this->hasMany('Category', 'parent_id');
}
}