Laravel 6 relationship output as array not object

The laravel 6.x returns the output as an array instead of an object when accessing the relationship in one pc it supports the array and on the other pc, it only supports the object.

class Category extends Model
{

    public function parent() {
        return $this->belongsTo(Category::class,'parent_id');
    }

    public function children() {
        return $this->hasMany(Category::class,'parent_id');
    }

}

on the blade page this can be accessed like below

 @foreach ($categories as $category)
   <tr>

     <td >{{$category->parent['name']}}</td>
   </tr>
 @endforeach

Upvotes: 0

Views: 457

Answers (1)

Alex
Alex

Reputation: 1638

Eloquent models implement ArrayAccess.

So $category->parent->name should work the same as $category->parent['name'].

It's not actually an array it just is accessible like one.

Upvotes: 1

Related Questions