【发布时间】: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