Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

raffelustig's avatar

No error printout despite no selected and more

In my Laravel 9 version of the pistol competition system we have an invoice system. Every shooter that signup to a competition have to pay a fee. I have problem with the invoice generating system. In this part of controller:

    public function store(Request $request)
    {
        $user = \Auth::user();
        $club = $user->Clubs->first();

        if ($club->user_has_role != 'admin') {
            return response()->json(_('Du behöver ha administratörsbehörighet för din förening för att göra en laganmälan'), 403);
        }

        $signupIds = null;
        $signupsSelected = ($request->exists('signup_ids'));
        if ($request->has('signup_ids')) {
            $signupIds = explode(',', $request->get('signup_ids'));
        }

        $teamIds = null;
        $teamsSelected = ($request->exists('team_ids'));
        if ($request->has('team_ids')) {
            $teamIds = explode(',', $request->get('team_ids'));
        }

        dump($signupsSelected);
        dump($signupIds);
        dump($teamsSelected);
        
        if($teamIds = ""){
            $teamIds ="false" ;
        }  
        dump($teamIds);
        // If all options are unchecked, display error message.
        if (($signupsSelected && ! $signupIds) && ($teamsSelected && ! $teamIds)) {
            return response()->json(['message'=>_('Du måste välja minst en rad att fakturera')], 422);
        }

        $this->invoices->createInvoicesForClub($club, $signupIds, $teamIds);

        return response()->json(['message'=>_('Dina fakturor är skapade')]);
    }

"Du måste välja minst en rad att fakturera" is the error text that should show. No error printout despite checkboxes not set in the blade here:

<div class="table-responsive">
        <table class="table table-hover table-bordered">
            <thead>
            <tr>
                <th width="10">Fakturera</th>
                <th>Användare</th>
                <th>Tävling</th>
                <th>Vapengrupp</th>
                <th class="text-right">Summa</th>
            </tr>
            </thead>
            <tbody>
                <tr ng-repeat="signup in club.signups">
                    <td><input type="checkbox" ng-click="invoices.toggleInvoiceSignup(signup)" checked></td>
                    <td><a ui-sref="club.users.show({user_id: signup.user.user_id})"><% signup.user.fullname %></a></td>
                    <td><a ui-sref="competition.clubsignups({id: signup.competitions_id})"><% signup.competition.date %> <% signup.competition.name %></a></td>
                    <td><% (signup.competition.championships_id) ? signup.weaponclass.classname_general : signup.weaponclass.classname %></td>
                    <td class="text-right"><% signup.registration_fee %></td>
                </tr>
            </tbody>
        </table>
    </div>

Picture of the blade output. Checkboxes to the left as seen. https://imgur.com/MYSpnNd

There is also a problem that if I check only one of the checkboxes, all are combined into a single invoice instead of only the on with checkbox activated. Here:

        if (($signupsSelected && ! $signupIds) && ($teamsSelected && ! $teamIds)) {
            echo('test');
            return response()->json(['message'=>_('Du måste välja minst en rad att fakturera')], 422);
        }
        dd($signupIds);
        $this->invoices->createInvoicesForClub($club, $signupIds, $teamIds);

        return response()->json(['message'=>_('Dina fakturor är skapade')]);
    }

the dd gives this with one checkbox marked:

array:1 [
  0 => "49"
]

with two:

array:2 [
  0 => "49"
  1 => "50"
]

I got a solution on the first problem. In the part:

       // If all options are unchecked, display error message.
        if (($signupsSelected && ! $signupIds) && ($teamsSelected && ! $teamIds)) {
            return response()->json(['message'=>_('Du måste välja minst en rad att fakturera')], 422);
        }

should be:

        // If all options are unchecked, display error message.
        if (($signupsSelected && (in_array("", $signupIds))) && ($teamsSelected &&(in_array("", $teamIds)))) {
            return response()->json(['message'=>_('Du måste välja minst en rad att fakturera')], 422);
        }

and then the error printout comes ok.

Bot the other problem o what can we do? Thanks in advance for your help.

0 likes
3 replies

Please or to participate in this conversation.