Search code examples
phplaravelforeachnested-loops

trying to nest foreach loop to remove items and create new array in laravel controller


So, I've managed to thoroughly confuse myself and any help would be greatly appreciated. What I'm trying to do is filter out items that are restricted by a user’s country (done in my controller).

My first query gets all items (posters in this case). My second item gets the restricted posters. This code works as planned.

I then try to create a nested loop to filter out the restricted posters from all posters by creating a new array and pushing the items that pass my if clause into my $unrestrictedPosters array. My if clause is where my code fails as I'm unable to properly access the key value in order to fulfil my if clause.

Btw, once I return $unrestrictedPosters that would then be use and looped through in my bade file.

controller code below

class PosterRestrictionController extends Controller
{
    public function index() 
    {
        $profile = app('App\Http\Controllers\DevelopmentController')->getActiveUser(Auth::id());
        $userCountry = $profile->country;
        $unrestrictedPosters = [];

        $allPosters = DB::table('posters')
        ->join('profiles', 'posters.user_id', '=', 'profiles.user_id')
        ->join('users', 'posters.user_id', '=', 'users.id')
        ->select('users.name',
                'profiles.surname',
                'profiles.country',
                'posters.id as poster_id',
                'posters.user_id',
                'posters.title',
                'posters.category',
            )
        ->orderBy('posters.id', 'asc')
        ->get();

        $restrictedPosters = DB::table('posters')
        ->join('poster_restrictions', 'poster_restrictions.poster_id', '=', 'posters.id')
        ->where('poster_restrictions.restricted_country_id', '=', $userCountry)
        ->select(
                'posters.id as poster_id',
                'poster_restrictions.poster_viewable',
                'poster_restrictions.restricted_country_id'
                )
        ->orderBy('posters.id', 'asc')
        ->get();

        

        foreach ($allPosters as $poster) {
            
            foreach ($restrictedPosters as $restrictedPoster) {

                    if ($restrictedPoster['poster_viewable'] != 0) {
                        
                        array_push($unrestrictedPosters, $poster);
                  
                }

            }
            
        }

        dd($unrestrictedPosters);
        //return $unrestrictedPosters;
    }
}

Solution

  • Why bother loopingit when you can restrict them in the query

    class PosterRestrictionController extends Controller
    {
        public function index() 
        {
            $profile = app('App\Http\Controllers\DevelopmentController')->getActiveUser(Auth::id());
            $userCountry = $profile->country;
    
            $unrestrictedPosters = DB::table('posters')
            ->join('profiles', 'posters.user_id', '=', 'profiles.user_id')
            ->join('users', 'posters.user_id', '=', 'users.id')
            ->leftJoin('poster_restrictions', function($join) use($userCountry) {
                $join->on('poster_restrictions.poster_id', '=', 'posters.id')
                    ->where('poster_restrictions.restricted_country_id', '=', $userCountry);
            })
            ->whereNull('poster_restrictions.restricted_country_id')
    
            ->select('users.name',
                    'profiles.surname',
                    'profiles.country',
                    'posters.id as poster_id',
                    'posters.user_id',
                    'posters.title',
                    'posters.category',
                )
            ->orderBy('posters.id', 'asc')
            ->get();
    
            dd($unrestrictedPosters);
            //return $unrestrictedPosters;
        }
    }