ItsMrSammeh's avatar

Eloquent Question / Pagination

Hey guys.

So basically, I have an int connected to my main user account, which will determine, which client they are under in our application(Which client they are working with in the business)

Each account can have multiple youtube channels added to them, and in one of the parts of our application, we need to grab all youtube accounts, via the clients MASTER client(The user that is basically above the current one, how we explained previously).

So to do this, I have to do this:

  $users = User::where([
            'head_network' => Auth::user()->getNetwork()->id
        ])->orderBy('id', 'DESC');
        $channels = [];
        if($users->count())
        {
            foreach ($users->get() as $user) {
                $channel = $user->getChannels;
                foreach ($channel as $ch) {
                    if($ch->network_verified == 1 && $ch->manager_verified == 1 && $ch->link_status == 0)
                    {
                        $channels[] = $channel;
                    }
                }
            }
        }

Now, as you can tell, I'd like to paginate this data. Now, I could go the custom route, but is that neccesary, I mean do I have to convert this to an array?

Is there a simple way to basically do something along the lines of YoutubeChannels::where('user.head_network', 1)

or something like this?

Also, as you can see I have the three checks $ch->network_verified == 1 && $ch->manager_verified == 1 && $ch->link_status == 0 These are needed, and it's bad practice to paginate, and then do these checks in the view, since I wont have my specific required items, plus... if a whole page doesnt equal these, we will have blank pages. Found this out the hard way lol.

I never fully understood the whole MySQL relationships where they connect like that in Queries, so would really appreciate some feedback on the best way to go about this!

Thanks guys!

0 likes
2 replies
Rwendel's avatar

How is your 'getChannels' defined? You can add where clauses to the result of an attribute that would result a collection. ie. I have an Organization object, with members. The same table is used for pending (approved = false) or active (approved = true). This is a many/many relationship with intermediate pivots, which is identical on both except one is filtered to active and the other filtered to pending.

   public function users()
    {
        return $this->belongsToMany('App\User','org_users','org_id','user_id')->withPivot('role','admin')->where('approved', 1);
    }

   public function pendingUsers()
    {
        return $this->belongsToMany('App\User','org_users','org_id','user_id')->withPivot('role','admin')->where('approved', 0);
    }

Please or to participate in this conversation.