Rahi
Rahi

Reputation: 7

Laravel 7 : Why I am Getting Only First Array? I want to Fetch All Category ID data

I am getting a issue while fetching array data in Laravel 7 here is my code https://i.sstatic.net/IZbg6.png

and the result is : https://i.sstatic.net/ByKaV.png

It is fetching only one array data. I don't know where i am missing.

If anybody know the error, please help me to solve this issue.

Below is my code ======================================

  $cat_id = $category->id;
   
$location = null;        

 $sites = \DB::select( 'SELECT id FROM sites WHERE category_id = ?', [ $category->id ]);


    $all = [  ];

            foreach( $sites as $s ) {
                $all[  ] = $s->id;
            }

            $sites = $all;
            
  $all_cat_id = implode(',', array_map('intval', $sites));
            
//   echo "<pre>";
//             print($all_cat_id);
//             die();            

       
$sites = Sites::withCount('reviews')->orderBy('reviews_count', 'desc')->where('id', [$all_cat_id])->paginate(10);

    
    return view('browse-category', [ 'activeNav' => 'home', 
    'reviews' => $reviews, 
    'sites' => $sites, 
    'category' => $category, 
    'all_categories' => $all_categories, 
    'location' => $location 
    ]);

Upvotes: 0

Views: 40

Answers (1)

Akhzar Javed
Akhzar Javed

Reputation: 628

$sites = Sites::withCount('reviews')->orderBy('reviews_count', 'desc')->whereIn('id', [$all_cat_id])->paginate(10);

You need to use whereIn() instead of where()

whereIn() checks column against array.

Upvotes: 1

Related Questions