【问题标题】:Laravel 4 whereIn and many to manyLaravel 4 whereIn 和多对多
【发布时间】:2014-06-12 11:30:10
【问题描述】:

我有一个标签系统,您可以在其中为照片和用户添加标签。

我有一个功能,用户可以添加他们喜欢的标签,并根据这些标签选择图像

但我的问题是我是 php 和 laravel 的一个非常大的初学者,我不知道如何将值传递给 whereIn 函数

型号

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

控制器

// 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);

当我通过时,我得到一个错误

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

比我尝试使用array_flatten() 来清理我的阵列

// 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);

这样可以正常工作,但不会返回正确的标签。

请问有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: php laravel relation where-in


    【解决方案1】:

    当然可以,我会提出一些建议。

    要获取用户模型,您只需使用$user = Auth::user()。

    要使用whereIn(),它需要一个用户 ID 的一维数组。 toArray() 函数将返回一个包含所有用户及其属性的关联数组的数组,因此它不会正常工作。为了得到你需要的东西,你应该使用lists('id')。

    最后一件真正帮助我的事情是,当你建立一个将返回对象集合的关系(hasMany,belongsToMany())时,将关系名称设为复数,所以在这种情况下你会将您的 tag() 函数修改为 tags()。

    因此,考虑到所有这些,这应该适合您。

    // 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);
    

    我建议修改你与...的关系,虽然不是很重要。

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

    【讨论】:

    • 非常感谢您的详细回复,真的很有帮助,谢谢
    猜你喜欢
    • 2017-07-30
    • 2019-11-06
    • 2015-08-22
    • 2018-07-15
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多