title: Laravel get post categories with pivot table as BelongsToMany()
body: <p>in my database i have <code>contents_categories</code> and <code>content</code> table with <code>BelongsToMany()</code> relation ship</p>
  
  <p><code>Contents</code>:</p>
  
  <pre><code>class Contents extends Model
  {
      protected $table = 'contents';
      protected $guarded = ['id'];
  
      public function categories()
      {
          return $this-&gt;belongsToMany(ContentCategories::class);
      }
  }
  </code></pre>
  
  <p><code>ContentCategories</code>:</p>
  
  <pre><code>class ContentCategories extends Model
  {
      protected $table = 'contents_categories';
      protected $guarded = ['id'];
  
      public function contents()
      {
          return $this-&gt;belongsToMany(Contents::class);
      }
  }
  </code></pre>
  
  <p>using this code to store data in <code>contents</code> table with selected multiple category work fine and that can be save into database and pivot table as <code>content_categories_contents</code></p>
  
  <pre><code>public function store(RequestContents $request)
  {
      $data = Contents::create([
          'title' =&gt; $request-&gt;title,
          'lang' =&gt; $request-&gt;_language,
          'type' =&gt; $request-&gt;type,
          'description' =&gt; $request-&gt;description,
          'featured_images' =&gt; '',
          'visit' =&gt; 0,
      ]);
  
      $data-&gt;categories()-&gt;attach($request-&gt;categories);
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/49bjT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/49bjT.png" alt="enter image description here"></a></p>
  
  <p>now! unfortunately when i try to use this code to get categories of content i dont have them in result:</p>
  
  <pre><code>public function show($id)
  {
      $content = Contents::find($id);
      $selected_categories = $content-&gt;categories();
      dd($selected_categories);
  }
  </code></pre>
  
  <p>result:</p>
  
  <pre><code>BelongsToMany {#208 ▼
    #table: "content_categories_contents"
    #foreignPivotKey: "contents_id"
    #relatedPivotKey: "content_categories_id"
    #parentKey: "id"
    #relatedKey: "id"
    #relationName: "categories"
    #pivotColumns: []
    #pivotWheres: []
    #pivotWhereIns: []
    #pivotCreatedAt: null
    #pivotUpdatedAt: null
    #using: null
    #accessor: "pivot"
    #query: Builder {#217 ▼
      #query: Builder {#209 ▶}
      #model: ContentCategories {#213 ▼
        #table: "contents_categories"
        #guarded: array:1 [▶]
        #connection: null
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: false
        +wasRecentlyCreated: false
        #attributes: []
        #original: []
        #changes: []
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: []
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #fillable: []
      }
      #eagerLoad: []
      #localMacros: []
      #onDelete: null
      #passthru: array:11 [▶]
      #scopes: []
      #removedScopes: []
    }
    #parent: Contents {#218 ▼
      #table: "contents"
      #guarded: array:1 [▶]
      #connection: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: false
      +wasRecentlyCreated: false
      #attributes: []
      #original: []
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
    #related: ContentCategories {#213 ▶}
  }
  </code></pre>