$pinData = Pin::select(
DB::raw("`pin`.*,`pin_reviews`.`review`,( 3959 * acos( cos(
radians('$lat') ) * cos( radians( lat ) ) * cos( radians( lng ) -
radians('$lng') ) + sin( radians('$lat') ) * sin( radians( lat ) )
) )
AS distance,COUNT(`pin_reviews`.`pin_id`) as
`totalUserRating`,SUM(`pin_reviews`.`rating`) /
(COUNT(`pin_reviews`.rating * 5)) as
`rating`,`ads`.`product_name`,`ads`.`id` as
'adId',`ads`.`notes`,`ads`.`add_status`,`ads`.`description` as
'adsDescription',`c`.`name` as 'catName',`c`.`id` as 'catId',`sc`.`id`
as 'subcatId', `sc`.`name` as 'subName',`ssc`.`id` as
'SubsubId',`ssc`.`name` as 'subsubName' " ));
$pinData->LeftJoin('pin_reviews','pin_reviews.pin_id','=',
'pin.id');
$pinData->LeftJoin('ads','ads.pin_id','=', 'pin.id');
$pinData->LeftJoin('category as c','c.id','=', 'ads.category_id');
$pinData->LeftJoin('category as sc','sc.id','=',
'ads.sub_category_id');
$pinData->LeftJoin('category as ssc','ssc.id','=',
'ads.sub_sub_category');
//$pinData->where('pin.status',1);
$pinData->groupBy('pin.id');
$pinData->Having('distance','<',1870);
$qcon = '';
// if search keyword
if($search!='')
{
$qcon.= " AND (pin.pin_name LIKE '%".$search."%'";
$qcon.= " OR pin.name_on_map LIKE '%".$search."%'";
$qcon.= " OR pin.description LIKE '%".$search."%'";
}
if($adStatus!='')
{
$qcon.= " AND ads.add_status='". $adStatus."' ";
}
$pinData->whereRaw('( 1 '.$qcon.')');
$pinData->OrderBy('distance');
$pinData->limit($search_settings->pins_appear_limit);
$pins = $pinData->get();