user3615613
user3615613

Reputation: 71

Laravel 5 eloquent many-to-many relationship issue

I'm creating a blog using Laravel 5 but I'm having a strange issue with a many-to-many (via pivot table) relationship. My DB setup is the following:

posts table:
id | bigint | 20 | PK | Not Null | Auto Increment
title | varchar | 255 | Not Null
body | text | Not Null
category_id | bigint | 20 | Not Null
created_at | timestamp | Not Null
updated_at | timestamp | Not Null
published | boolean | Not Null
deleted_at | timestamp | Not Null

tags table:
id | bigint | 20 | PK | Not Null | Auto Increment
name | varchar | 255 | Not Null
created_at | timestamp | Not Null
updated_at | timestamp | Not Null

post_tag pivot table:
id | bigint | 20 | PK | Not Null | Auto Increment
post_id | bigint | 20 | Not Null
tag_id | bigint | 20 | Not Null
created_at | timestamp | Not Null
updated_at | timestamp | Not Null

Following are extracted from my models:
Post

public function tags(){
    return $this->belongsToMany('App\Tag');
}

Tag

public function posts(){
    return $this->belongsToMany('App\Post');
}

Now on the view if I do this:

var_dump($post->tags->first()->name);

I'm returned the correct tag associated to the post, but obviously the post can have more tags so what I'm trying to do is a for each loop on $post->tags.
Anyway if I try to do it, it seems the result is not usable and the loop never start.
Then I've tried to look at what I'm returned doing this:

var_dump($post->tags);

And I'm returned the following:

object(Illuminate\Database\Eloquent\Collection)#199 (1) { ["items":protected]=> array(1) { [0]=> object(App\Tag)#200 (21) { ["table":protected]=> string(4) "tags" ["fillable":protected]=> array(1) { [0]=> string(4) "name" } ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(4) { ["id"]=> string(1) "2" ["name"]=> string(3) "Tag" ["created_at"]=> string(19) "2015-02-25 15:26:29" ["updated_at"]=> string(19) "2015-02-25 15:26:29" } ["original":protected]=> array(6) { ["id"]=> string(1) "2" ["name"]=> string(3) "Tag" ["created_at"]=> string(19) "2015-02-25 15:26:29" ["updated_at"]=> string(19) "2015-02-25 15:26:29" ["pivot_post_id"]=> string(1) "6" ["pivot_tag_id"]=> string(1) "2" } ["relations":protected]=> array(1) { ["pivot"]=> object(Illuminate\Database\Eloquent\Relations\Pivot)#193 (24) { ["parent":protected]=> object(App\Post)#198 (22) { ["table":protected]=> string(5) "posts" ["dates":protected]=> array(1) { [0]=> string(10) "deleted_at" } ["fillable":protected]=> array(4) { [0]=> string(5) "title" [1]=> string(4) "body" [2]=> string(11) "category_id" [3]=> string(9) "published" } ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(8) { ["id"]=> string(1) "6" ["title"]=> string(4) "Test" ["body"]=> string(24) " Test

" ["category_id"]=> string(1) "1" ["created_at"]=> string(19) "2015-02-25 15:49:20" ["updated_at"]=> string(19) "2015-02-25 15:49:20" ["published"]=> string(1) "1" ["deleted_at"]=> NULL } ["original":protected]=> array(8) { ["id"]=> string(1) "6" ["title"]=> string(4) "Test" ["body"]=> string(24) " Test

" ["category_id"]=> string(1) "1" ["created_at"]=> string(19) "2015-02-25 15:49:20" ["updated_at"]=> string(19) "2015-02-25 15:49:20" ["published"]=> string(1) "1" ["deleted_at"]=> NULL } ["relations":protected]=> array(2) { ["likes"]=> object(Illuminate\Database\Eloquent\Collection)#197 (1) { ["items":protected]=> array(0) { } } ["tags"]=> RECURSION } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "" } ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["forceDeleting":protected]=> bool(false) } ["foreignKey":protected]=> string(7) "post_id" ["otherKey":protected]=> string(6) "tag_id" ["guarded":protected]=> array(0) { } ["connection":protected]=> NULL ["table":protected]=> string(8) "post_tag" ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(false) ["attributes":protected]=> array(2) { ["post_id"]=> string(1) "6" ["tag_id"]=> string(1) "2" } ["original":protected]=> array(2) { ["post_id"]=> string(1) "6" ["tag_id"]=> string(1) "2" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) } } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "" } ["dates":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) } } }

I've highlighted the part that for me is giving me the problem, for some reason it seems it is having some kind of query recursion which I can't understand why it happens.
Any Idea?

Thanks,
Matteo

Upvotes: 1

Views: 1893

Answers (2)

MrMAG
MrMAG

Reputation: 1264

Maybe your autoload_classmap.php wasn't up to date.

This file is regenerated on each dump-autoload. If you have a new class somewhere in your project it will not be loaded unless it is included in autoload_classmap

composer dump-autoload

This may happened to you, maybe automatically by installing or updating package or something.

Good tip: If you face a problem in Laravel, try to run composer dump-autoload, which often may fix some problems.

Upvotes: 0

Anonymouse
Anonymouse

Reputation: 35

You are getting the query collection. You may convert collection to array using ->toArray() so you can get only the needed data:

$post->tags->toArray();

Upvotes: 2

Related Questions