LaravelのEloquent ORM(hasについて)

Laravel4.1です。

参考は公式よりEloquent

こんなことがやりたいと思った。 タグ検索をandとorでしたい。

こんなことも簡単にできるLaravelです。

Illuminate\Database\Eloquent\Builderを参照するといいですよ。

    /**
     * Add a relationship count condition to the query.
     *
     * @param  string  $relation
     * @param  string  $operator
     * @param  int     $count
     * @param  string  $boolean
     * @param  \Closure  $callback
     * @return \Illuminate\Database\Eloquent\Builder|static
     */
    public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $callback = null)

タグを持っている

Post::has('tags')->get();

タグIDの1と2を持っている

$tags = [1,2];
$count = count($tags);
Post::whereHas('tags',function($q) use($tags){
    $q->whereIn('tags.id',$tags);
},'=',$count)->get()

タグIDの1か2を持っている

$tags = [1,2];
$count = count($tags);
Post::whereHas('tags',function($q) use($tags){
    $q->whereIn('tags.id',$tags);
},'=>',1)->get()
2014-03-28 22:21:00( 更新 2014-03-28 22:21:00 )
Tag laravel | eloquent | orm