vanyahuk
@vanyahuk

Как написать в laravel cложный запрос к mysql?

здраствуйте, есть такой запрос к бд

select *, ( 

        SELECT COUNT(*) from `filter_products` where `product_id` in (
        
        select `category_products`.`product_id` from `category_products` where `category_products`.`category_id` = 1
        
        group by `category_products`.`product_id`
        
        )  and `filters` . `id` = `filter_products` . `filter_id`
        
        ) as total from `filters` where `id` in (
        
        select `filter_id` from `filter_products` where `product_id` in (
        
        select `category_products`.`product_id` from `category_products` where `category_products`.`category_id` = 1
        
        ) group by `filter_id`) 
        
        and `filter_group_id` = 2 order by `title` asc


как его написать с помощью orm?

вот есть часть кода, но count никак немогу воткнуть в орм

public function filters( $filters = null )
    {


    	return  Filter::/*distinct('total', function( $query ) use ($filters){

            $query  -> select('product_id')

                    -> from( 'filter_products' )

                    -> whereIn( 'product_id', function( $q ) use ($filters){

                        $q -> select('category_products.product_id')

                            -> from('category_products');

                        $q  -> where('category_products.category_id', $this -> category_id )

                            -> groupBy('category_products.product_id');

                    } ) -> where('filters.id', '=', 'filter_products.filter_id') -> count();



            })*/->whereIn('id', function( $query ) use ( $filters ) {

    		   		$query->select('filter_id')
              			  
              			  ->from('filter_products')
              		      
              		      ->whereIn('product_id', function( $q )  use ( $filters ) {

              		      					$q -> select('category_products.product_id')

              		      					   -> from('category_products');


              		      					// get ajax filters
              		      					if( $filters != null )
                                            {

                                                $q -> leftJoin('filter_products', 'category_products.product_id' , '=','filter_products.product_id' )

                                                    -> whereIn('filter_products.filter_id', $filters) ;

                                            }

              		      					 $q  -> where('category_products.category_id', $this -> category_id ) ;

              		      }) 

              		         -> groupBy('filter_id') ;

    	}) -> where('filter_group_id', $this -> filter_group_id ) -> orderBy('title', 'ASC') -> get() ;
    }
  • Вопрос задан
  • 143 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы