RFFRED
216
2
Laravel

Where should this code go??

Posted 3 months ago by RFFRED

Hi,

Where should I put this code?

    //counts the number of roles in the DB
        $count = Role::count();

        //if roles found
        if ($count > 0){
            $roles = Role::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('name', 'id');
        } else {
            $roles = [];
        }

This code returns all the user roles from a table and will be used to create a dropdown select form element. I have this code used in a few places in my app and I feel I could put it in a function. but where? I don't want to put in any controllers as I don't feel it is the right place for it.

I have read a little about services. Is it what I should use?

Please sign in or create an account to participate in this conversation.