Laravel 4 whereIn and many to many

Levi

I have a tag system, where you can add tags to photos and users.

I have a function where the users are able to add their favorite tags, and select images based on those tags

But my problem i am a really big beginner with php and laravel and i do not know how to pass the values to the whereIn function

Model

public function tag()
    {
        return $this->belongsToMany('Tag', 'users_tag');
    }

Controller

// get the logged in user
        $user = $this->user->find(Auth::user()->id);

        // get tags relation
        $userTags = $user->tag->toArray();

        // select photos based on user tags
        $photos = Photo::whereHas('tag', function($q) use ($userTags)
        {
            $q->whereIn('id', $userTags);

        })->paginate(13);


        $trendyTags = $this->tag->trendyTags();

        $this->layout->title = trans('tag.favorite');
        $this->layout->content = View::make('main::favoritetags')
                                ->with('user', $user)
                                ->with('photos', $photos)
                                ->with('trendyTags', $trendyTags);

When i pass i get an error

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array 

than i tried to use array_flatten() to clean my array

// get the logged in user
        $user = $this->user->find(Auth::user()->id);

        // get tags relation
        $userTags =array_flatten($user->tag->toArray());

        // select photos based on user tags
        $photos = Photo::whereHas('tag', function($q) use ($userTags)
        {
            $q->whereIn('id', $userTags);

        })->paginate(13);


        $trendyTags = $this->tag->trendyTags();

        $this->layout->title = trans('tag.favorite');
        $this->layout->content = View::make('main::favoritetags')
                                ->with('user', $user)
                                ->with('photos', $photos)
                                ->with('trendyTags', $trendyTags);

This way it works but not returning the correct tags.

Could please someone could lend me a hand on this?

user1669496

Sure thing and I'll make a couple recommendations.

To get the user model, you simply have to use $user = Auth::user().

To use whereIn(), it's expecting a 1 dimensional array of user id's. The toArray() function is going to return an array of associative arrays containing all the users and their properties, so it's not going to work quite right. To get what you need, you should use lists('id').

And one last thing that has really helped me is when you are setting up a relation that's going to return a collection of objects (hasMany, belongsToMany()), make the relation name plurual, so in this case you would modify your tag() function to tags().

So with all that in mind, this should work for you.

// get the logged in user
$user = Auth::user();

// get tags relation
$userTags = $user->tags()->lists('id');

// select photos based on user tags
$photos = Photo::whereHas('tags', function($q) use ($userTags)
{
    $q->whereIn('id', $userTags);

})->paginate(13);

$trendyTags = $this->tags->trendyTags();

$this->layout->title = trans('tag.favorite');
$this->layout->content = View::make('main::favoritetags')
                        ->with('user', $user)
                        ->with('photos', $photos)
                        ->with('trendyTags', $trendyTags);

And I'd suggest to modify your relation to... though not hugely important.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel 4.1 WhereIn w/ Many to Many Relation and Conditionals?

From Dev

Laravel 4: many to many (insertion)

From Dev

Laravel 4 many to many relationships

From Dev

Laravel 4: many to many (insertion)

From Dev

Laravel 4 Many to Many with Eloquent and checkboxes

From Dev

Laravel 4 - Chaining Many-to-Many relationships

From Dev

Eager loading with many to many relationship in laravel 4

From Dev

Laravel 4 - Many to Many - pivot table not updating

From Dev

Laravel - Many to Many relationship

From Dev

Laravel - Many To Many

From Dev

Laravel Many to Many relation

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Many to Many laravel

From Dev

Laravel 4 : How to get selected/specific columns in a many to many relationship?

From Dev

Remove reationship from many-to-many models in laravel 4

From Dev

Laravel 4 : Authentication when having a many to many relationship

From Dev

In Laravel 4 Using creating a many to many relationship between 2 databases

From Dev

Laravel 4 many to many relationship error when no records match

From Dev

laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many

From Dev

one to many relationship help in laravel 4

From Dev

Pass many optional parameters to route in Laravel 4

From Dev

Laravel 4 Encryption: how many characters to expect

From Dev

Too Many Connections w/ Laravel 4 and MySQL

From Dev

Laravel 4 Seeding many rows, artisan stops

From Dev

Laravel 4 Seeding many rows, artisan stops

From Dev

Laravel 4 not returning one-to-many

From Dev

Laravel 4 Encryption: how many characters to expect

From Dev

Laravel eloquent many to many to many model

Related Related

HotTag

Archive