Laravel 如何访问元素进行雄辩的查询

Laravel 如何访问元素进行雄辩的查询,laravel,eloquent,Laravel,Eloquent,属性模型 类后属性扩展模型 { 受保护的$table='post_properties'; 公共职能职位 { 返回$this->belongsTo'App\Post'; } } 后模型 类后扩展模型 { 公共功能后属性 { 返回$this->hasMany'App\PostProperty'; } } 桌子 样本输出 $pv这是我的阵列:示例“car”、“25”、“foo” 质疑 $posts=Post::带有'postproperty' ->其中'type_id','=',$type ->

属性模型

类后属性扩展模型 { 受保护的$table='post_properties'; 公共职能职位 { 返回$this->belongsTo'App\Post'; } } 后模型

类后扩展模型 { 公共功能后属性 { 返回$this->hasMany'App\PostProperty'; } } 桌子

样本输出

$pv这是我的阵列:示例“car”、“25”、“foo”

质疑

$posts=Post::带有'postproperty' ->其中'type_id','=',$type ->其中函数$q使用$title,$pv{ $q->orWhere'title'、'like'、'%'.$title.'% ->OrWhere函数$qq使用$pv{ 对于$i=0;$iorwhere'property_value','like','%%.$pv[$i].'; }; } ->得到; 我需要访问postproperty的元素以放入查询条件。

        $posts = Post::where('type_id','=',$type)
        ->leftJoin('post_properties','posts.id','=','post_properties.post_id')
        ->where(function($q)use($title,$pv){
            $q->orwhere('title','like','%'.$title.'%');
            for ($i=0;$i<count($pv);$i++)
                $q->orwhere('property_value','like','%'.$pv[$i].'%');
        })
        ->groupBy('id')
        ->get();

更好

您是想快速加载属性关系吗?我想创建帖子的高级搜索和帖子属性是加载后属性关系。并插入Elount中的何处,用于筛选查询
        $posts = DB::table('posts')
        ->leftJoin('post_properties','posts.id','=','post_properties.post_id')
        ->select('posts.*')
        ->where('posts.type_id','=',$type)
       ->where('posts.verified_at','!=',null)
        ->where(function($q)use($title,$pv){
            $q->orwhere('posts.title','like','%'.$title.'%');
            for ($i=0;$i<count($pv);$i++)
                $q->orwhere('post_properties.property_value','like','%'.$pv[$i].'%');
        })
        ->distinct('posts.id');