Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Batman55's avatar

belongsToMany returns a belongsToMany, not a collection

I am using Laravel 8.

I had issues with this on "Display All Tags Under Each Article" here: https://laracasts.com/series/laravel-6-from-scratch/episodes/32

I never did get the above resolved. I am also having issues here currently:

public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }
$tags = \App\Models\Article::first()->tags;

TypeError: Return value of App\Models\Article::tags() must be an instance of Illuminate\Database\Eloquent\Collection, instance of Illuminate\Database\Eloquent\Relations\BelongsToMany returned

If I run this, which should return an eloquent collection:

public function tags()
    {
        return $this->belongsToMany(Tag::class)->get();
    }

The output in tinker is:

$tags = \App\Models\Article::first()->tags;

LogicException with message 'App\Models\Article::tags must return a relationship instance.'
0 likes
6 replies
fylzero's avatar

@batman55 Try...

public function tags()
{
    return $this->belongsToMany('App\Models\Tag');
}
1 like
Batman55's avatar

Still getting the error with that solution.

 Return value of App\Models\Article::tags() must be an instance of Illuminate\Database\Eloquent\Collection, instance of Illuminate\Database\Eloquent\Relations\BelongsToMany returned

With die and dump this is what comes out for tinker:

public function tags()
{
    return $this->belongsToMany('App\Models\Tag');
}
>>> $tags = \App\Models\Article::first()->tags;
Illuminate\Database\Eloquent\Collection {#3335
  #items: array:3 [
    0 => App\Models\Tag {#4056
      #connection: "mysql"
      #table: "tags"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [
        "id" => 1
        "name" => "Laravel"
        "created_at" => "2021-01-02 18:31:25"
        "updated_at" => "2021-01-02 18:31:26"
      ]
      #original: array:6 [
        "id" => 1
        "name" => "Laravel"
        "created_at" => "2021-01-02 18:31:25"
        "updated_at" => "2021-01-02 18:31:26"
        "pivot_article_id" => 1
        "pivot_tag_id" => 1
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [
        "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#4278
          +incrementing: false
          #guarded: []
          #connection: "mysql"
          #table: "article_tag"
          #primaryKey: "id"
          #keyType: "int"
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:2 [
            "article_id" => 1
            "tag_id" => 1
          ]
          #original: array:2 [
            "article_id" => 1
            "tag_id" => 1
          ]
          #changes: []
          #casts: []
          #classCastCache: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: false
          #hidden: []
          #visible: []
          #fillable: []
          +pivotParent: App\Models\Article {#3651
            #fillable: array:3 [
              0 => "title"
              1 => "excerpt"
              2 => "body"
            ]
            #connection: "mysql"
            #table: "articles"
            #primaryKey: "id"
            #keyType: "int"
            +incrementing: true
            #with: []
            #withCount: []
            #perPage: 15
            +exists: true
            +wasRecentlyCreated: false
            #attributes: array:7 [
              "id" => 1
              "user_id" => 1
              "title" => "Learn Laravel"
              "excerpt" => "Ut maxime id veniam et fuga quaerat."
              "body" => "aerwfasdfasef "
              "created_at" => "2021-01-02 01:56:29"
              "updated_at" => "2021-01-02 01:56:29"
            ]
            #original: array:7 [
              "id" => 1
              "user_id" => 1
              "title" => "Learn Laravel"
              "excerpt" => "Ut maxime id veniam et fuga quaerat."
              "body" => "aerwfasdfasef "
              "created_at" => "2021-01-02 01:56:29"
              "updated_at" => "2021-01-02 01:56:29"
            ]
            #changes: []
            #casts: []
            #classCastCache: []
            #dates: []
            #dateFormat: null
            #appends: []
            #dispatchesEvents: []
            #observables: []
            #relations: []
            #touches: []
            +timestamps: true
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
          }
          #foreignKey: "article_id"
          #relatedKey: "tag_id"
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [
        0 => "*"
      ]
    }
    1 => App\Models\Tag {#4123
      #connection: "mysql"
      #table: "tags"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [
        "id" => 2
        "name" => "PHP"
        "created_at" => "2021-01-02 18:31:36"
        "updated_at" => "2021-01-02 18:31:37"
      ]
      #original: array:6 [
        "id" => 2
        "name" => "PHP"
        "created_at" => "2021-01-02 18:31:36"
        "updated_at" => "2021-01-02 18:31:37"
        "pivot_article_id" => 1
        "pivot_tag_id" => 2
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [
        "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#4214
          +incrementing: false
          #guarded: []
          #connection: "mysql"
          #table: "article_tag"
          #primaryKey: "id"
          #keyType: "int"
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:2 [
            "article_id" => 1
            "tag_id" => 2
          ]
          #original: array:2 [
            "article_id" => 1
            "tag_id" => 2
          ]
          #changes: []
          #casts: []
          #classCastCache: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: false
          #hidden: []
          #visible: []
          #fillable: []
          +pivotParent: App\Models\Article {#3651}
          #foreignKey: "article_id"
          #relatedKey: "tag_id"
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [
        0 => "*"
      ]
    }
    2 => App\Models\Tag {#4279
      #connection: "mysql"
      #table: "tags"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [
        "id" => 3
        "name" => "Educational"
        "created_at" => "2021-01-02 18:31:49"
        "updated_at" => "2021-01-02 18:31:47"
      ]
      #original: array:6 [
        "id" => 3
        "name" => "Educational"
        "created_at" => "2021-01-02 18:31:49"
        "updated_at" => "2021-01-02 18:31:47"
        "pivot_article_id" => 1
        "pivot_tag_id" => 3
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [
        "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#4212
          +incrementing: false
          #guarded: []
          #connection: "mysql"
          #table: "article_tag"
          #primaryKey: "id"
          #keyType: "int"
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:2 [
            "article_id" => 1
            "tag_id" => 3
          ]
          #original: array:2 [
            "article_id" => 1
            "tag_id" => 3
          ]
          #changes: []
          #casts: []
          #classCastCache: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: false
          #hidden: []
          #visible: []
          #fillable: []
          +pivotParent: App\Models\Article {#3651}
          #foreignKey: "article_id"
          #relatedKey: "tag_id"
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [
        0 => "*"
      ]
    }
  ]
}
>>> TypeError: Return value of App\Models\Article::tags() must be an instance of Illuminate\Database\Eloquent\Collection, instance of Illuminate\Database\Eloquent\Relations\BelongsToMany returned
Batman55's avatar

The only place it is being called is in the blade template. The earlier long printout is from tinker.

@foreach($article->tags as $tag)
<a href="/articles?tag={{ $tag->name }}"> {{ $tag->name }}</a>
@endforeach
fylzero's avatar

@batman55 Do you get the error if you comment or remove this code? That might be the problem.

1 like

Please or to participate in this conversation.