Debug School

rakesh kumar
rakesh kumar

Posted on

laravel Error:array_intersect_key(): Argument #1 ($array) must be of type array, null given

My code is

Image description

 $filteredSocialPrice = array_intersect_key($socialPrice, array_flip($convertedArray));
Enter fullscreen mode Exit fullscreen mode
       $fiteringdata = $fiteringdata->map(function ($item) use ($convertedArray) {
           $socialPrice = json_decode($item->social_price, true);

           // Filter the social_price based on the social sites in convertedArray
           $filteredSocialPrice = array_intersect_key($socialPrice, array_flip($convertedArray));

           // Update the social_price field
           $item->social_price = json_encode($filteredSocialPrice);

           return $item;
       });
Enter fullscreen mode Exit fullscreen mode
    $data =DB::connection('payments')->table("social_url")
            ->leftJoin('countries', 'social_url.country_id', '=', 'countries.country_id')
            ->leftJoin('states', 'social_url.state_id', '=', 'states.state_id')
            ->leftJoin('addcarts', function($join) use ($login_email) {
                $join->on('social_url.user_id', '=', 'addcarts.influencer_admin_id')
                    ->where('addcarts.admin_email', '=', $login_email);
            })          
            ->leftJoin('cities', 'social_url.city_id', '=', 'cities.city_id')                    
            ->where('social_url.user_email', '<>', $login_email)

            ->where('social_url.state_id', $state_id)
            ->select(
                'social_url.user_id',
                DB::raw('MAX(social_url.id) as max_id'),
                'social_url.*',
                'addcarts.influencer_admin_id',
                'addcarts.cart_socials',
                'addcarts.admin_id',
                'countries.country_name',
                'states.state_name',
                'cities.city_name'
            )
            ->groupBy('social_url.user_id')
            ->orderBy('id', 'desc')
            ->get();
Enter fullscreen mode Exit fullscreen mode

Solution
The error you're encountering suggests that the variable $socialPrice is null or not an array. To resolve this issue, you should check whether $socialPrice is a valid array before using array_intersect_key. Here's an updated version of your code with a check for $socialPrice:

whereNotNull('social_url.social_price')
Enter fullscreen mode Exit fullscreen mode
$data =DB::connection('payments')->table("social_url")
                ->leftJoin('countries', 'social_url.country_id', '=', 'countries.country_id')
                ->leftJoin('states', 'social_url.state_id', '=', 'states.state_id')  
                ->leftJoin(DB::raw('(SELECT MAX(id) AS max_id, influencer_admin_id, cart_socials, admin_id FROM addcarts GROUP BY influencer_admin_id) addcarts'), function($join)
                {
                    $join->on('social_url.user_id', '=', 'addcarts.influencer_admin_id');
                })        
                ->leftJoin('cities', 'social_url.city_id', '=', 'cities.city_id')     
                ->whereNotNull('social_url.social_price')
                ->select(
                    'social_url.user_id',
                    DB::raw('MAX(social_url.id) as max_id'),
                    'social_url.*',
                    'addcarts.influencer_admin_id',
                    'addcarts.cart_socials',
                    'addcarts.admin_id',
                    'countries.country_name',
                    'states.state_name',
                    'cities.city_name'
                )
                ->groupBy('social_url.user_id')
                    ->orderBy('id', 'desc')

                ->get();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)