title: Laravel : How to access many to many relationship data Ask
body: <p>I have category and subcategory table with many to many relationship</p>
  
  <pre><code>class Category extends Model {
  
      protected $table='categories';
      protected $fillable=['name'];
      public function subcategories() {
          return $this-&gt;belongsToMany('App\Modules\Subcategory\Models\Subcategory', 'categories_subcategories', 'category_id', 'subcategory_id');
      }
  }
  </code></pre>
  
  <p>Subcategory </p>
  
  <pre><code>class Subcategory extends Model {
  
      protected $table='subcategories';
      protected $fillable=['name'];
  
      public function categories()
      {
          return $this-&gt;belongsToMany('App\Modules\Category\Models\Category', 'categories_subcategories', 'subcategory_id', 'category_id');
      }
  
  }
  </code></pre>
  
  <p>in controller</p>
  
  <pre><code>public function catSubList()
      {
          $subcategories = Subcategory::with('categories')-&gt;get();
          return view('Subcategory::category_subcategory',compact('subcategories'));
      }
  </code></pre>
  
  <p>But in view when i tried to access the data with following view</p>
  
  <pre><code>@foreach($subcategories as $row) 
                      &lt;td&gt;{{$i}}&lt;/td&gt;
                      &lt;td&gt;{{$row-&gt;name}}&lt;/td&gt;  
                      &lt;td&gt;{{$row-&gt;categories-&gt;name}}&lt;/td&gt;
  
  @endforeach
  </code></pre>
  
  <p>I got the error like :</p>
  
  <p>ErrorException in Collection.php line 1527: Property [name] does not exist on this collection instance.
  How do i access <code>$row-&gt;categories-&gt;name</code> ? anyone with the suggestion please?</p>