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('AppHttpControllersDevelopmentController')->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;
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Why bother loopingit when you can restrict them in the query

class PosterRestrictionController extends Controller
{
    public function index() 
    {
        $profile = app('AppHttpControllersDevelopmentController')->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;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x