Levi
Levi

Reputation: 309

Laravel 4 whereIn and many to many

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?

Upvotes: 1

Views: 595

Answers (1)

user1669496
user1669496

Reputation: 33058

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');
}

Upvotes: 4

Related Questions