0

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');
    }

}
Prajwol Onta
  • 1,448
  • 5
  • 21
  • 48
  • You may try `$category->parent->name` or `$category->parent()->get()->name` – Razor Jul 11 '14 at 11:27
  • @Razor `$category->parent->name` gives `Trying to get property of non-object` and $category->parent()->get()->name give `Undefined property: Illuminate\Database\Eloquent\Collection::$name` – Prajwol Onta Jul 11 '14 at 11:34
  • What's your relation function look like? `{{ $category->parent }}` shouldn't be outputting the entire model's json. – user1669496 Jul 11 '14 at 11:46
  • @user3158900 please check the edits. – Prajwol Onta Jul 11 '14 at 11:54
  • 1
    Some of your categories don't have a parent, they are root nodes, so you must check it `$category->parent` is not null first, then access its properties. `get()` returns collection, what you want is `first()` that returns single model. – Jarek Tkaczyk Jul 11 '14 at 12:00
  • Also, check this one since it's exactly the same scenario to what you're working on http://stackoverflow.com/questions/24672629/laravel-orm-from-self-referencing-table-get-n-level-hierarchy-json/24679043#24679043 – Jarek Tkaczyk Jul 11 '14 at 12:04
  • @deczo yes you were right thank you .. but when i did this `{{ isset($category->parent) ? $category->parent()->get()->name : 'No parent' }}` its giving me `No parent` to even that has parent – Prajwol Onta Jul 11 '14 at 12:07
  • @deczo also could you please write the answer where i can tick as correct answer? – Prajwol Onta Jul 11 '14 at 12:09

1 Answers1

2

Some of your categories don't have a parent, they are root nodes, so you must check it $category->parent first, then access its properties.

Now, don't use isset since relation is not set as a property on the object. Use this instead:

if (count($category->parent)) // do something

// or is_null
if (is_null($category->parent));

Also get() returns collection, what you want is first() that returns single model.

I suggest you read this answer about recursive relations to make working with your setup easier: Laravel ORM from self referencing table get N level hierarchy JSON

And also this one, so you know why I suggest count instead of is_null to check related model: Laravel check if related model exists

Community
  • 1
  • 1
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157