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

raffelustig's avatar

Problems to make a first post in database table

In our system for pistol competition we have also an invoice system so when a shooter apply för a competition he is also to pay a fee. Normally he pays the club where he is connected to. When the organizer for the club is close to the competition date he will create the invoices för the shooters. In our previous system under Laravel 5.3 it works of course. But here with Laravel 7 there is problems.

This is the part of the program where the invoice number is created:

    }

    static public function GenerateInvoiceNumber($recipient_type, $recipient_id)
    {
        $latestInvoiceNr = Invoice::where('sender_type', $recipient_type)->where('sender_id', $recipient_id)->orderBy('invoice_nr','desc')->first();
    
        dump($latestInvoiceNr); //dump to see what comes out.
        //This is for Laravel 7
        if(is_countable($latestInvoiceNr)): //If the DB table is empty it should return 1500, but error "Trying to get property 'invoice_nr' of non-object"
            //In version 5.3 we have "if(count($latestInvoiceNr)):" and it works perfect
            return 1500; //The very first invoice shall have this number like 4520221500 (45 club nr, 2022 year)
        else:
            return $latestInvoiceNr->invoice_nr+1; //Following invoices is returned here (the second one like 4520221501)
        endif;
    }

Trying to get property 'invoice_nr' of non-object is the error for the "if(is_countable(..." statement. In the beginning the database table is empty. If I change to "if($latestInvoiceNr == NULL): I can suceed to make the first invoice but I am getting the correct invoice number 1500 but also a second one, 1501 that is empty, i.e. no value like 200 kr.

What shall I look for to solve this. Please advice if you want more information. Regards, Ralph in Sweden

0 likes
24 replies
undeportedmexican's avatar

I would simplify that query by just doing:

$latestInvoiceNr = Invoice::where('sender_type', $recipient_type)
													->where('sender_id', $recipient_id)
													->max('invoice_nr')

This way you automatically get the max NR and can just do: $latestInvoiceNr + 1

I think this should work.

Edit: In your case, since you want to start with 1500 I would probably do:

if($latestInvoiceNr){
			return $latestInvoiceNr + 1;
} else {
			return 1500;
}
raffelustig's avatar

@undeportedmexican All that works as if I only change if(is_countable($latestInvoiceNr)): to: if($latestInvoiceNr == NULL): But either comes with two invoices, the second empty which I don't want.:

2022-04-18	7320221502	Eslövs Skyttegilles Pistolklubb	Ej betald	1	0	200.00	 Ladda ner  Visa
2022-04-18	7320221503	Eslövs Skyttegilles Pistolklubb	Ej betald	0	0	0.00	 Ladda ner  Visa
2022-04-18	7320221500	Eslövs Skyttegilles Pistolklubb	Ej betald	1	0	200.00	 Ladda ner  Visa
2022-04-18	7320221501	Eslövs Skyttegilles Pistolklubb	Ej betald	0	0	0.00	 Ladda ner
raffelustig's avatar

So the question maybe will be "How to get only one invoice" :-)

undeportedmexican's avatar

@raffelustig I think the second invoice is an unrelated issue to your first one.

You have a nested loop. (First looping $signupsGrupedBySender then inside it looping $signups)

Check what you have in $signups->each(... that's the only reason I can see why you would be creating an invoice with the amount of 0, since you use that information to calculate the amount.

Snapey's avatar

You realise the danger in this code if you have more than one user? You get the latest number, but by the time you return the number, another user might have got the same number, and now you have a duplicate invoice number

I don't see how this code ever worked? You are returning an invoice record, but then treating it like its a single number?

Of course your code fails if the table is empty since $latestInvoiceNr will be null if there is no record.

raffelustig's avatar

@Snapey Well, there can actually only be one user. It's separated by the club number. One club which number is 45 will have the first invoice number 4520221500, the second 4520221501 and so on. Another club with number 73 the first invoice will be 7320221500 so it's cool. And yes, when the table is empty, in Laravel 5.3 version the statement if(count($latestInvoiceNr)): accepts null and returned 1500. But here in Laravel 7 it does not, it says "count(): Parameter must be an array or an object that implements Countable" whatever invoice number may be.

raffelustig's avatar

I can say this. If I instead of if(is_countable($latestInvoiceNr)): I use: if($latestInvoiceNr == NULL): it works but with a side effect that I'm getting an extra invoice that's empty. An I don't want that.

raffelustig's avatar

@Snapey I don't now what you mean, but in the club there can be several shooters signed for a competition and it means that when creating invoices it can be 5 or 10 or more invoices created at the same time.

raffelustig's avatar

Here is a dump dump($latestInvoiceNr);

    if($latestInvoiceNr == NULL):
        return 1500; 

from scratch where the first invoice is 1500:

 ------------ --------------------------------- 
  date         Mon, 18 Apr 2022 21:51:46 +0200  
  controller   "ClubInvoicesController"         
  source       Invoice.php on line 187          
  file         app/Models/Invoice.php           
 ------------ --------------------------------- 

App\Models\Invoice^ {#1634
  #table: "invoices"
  #fillable: array:1 [
    0 => "paid_at"
  ]
  #appends: array:10 [
    0 => "sum_amount"
    1 => "currency"
    2 => "signups_count"
    3 => "teams_count"
    4 => "invoice_date"
    5 => "payment_status"
    6 => "recipient_address_combined"
    7 => "sender_address_combined"
    8 => "recipient_object"
    9 => "sender_object"
  ]
  #hidden: array:11 [
    0 => "created_at"
    1 => "updated_at"
    2 => "deleted_at"
    3 => "created_by"
    4 => "invoice_nr"
    5 => "payment_gateway"
    6 => "payment_reference"
    7 => "recipient_type"
    8 => "sender_type"
    9 => "recipient_id"
    10 => "sender_id"
  ]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:29 [
    "id" => 70
    "invoice_nr" => 1500
    "invoice_reference" => "7320221500"
    "expiration_date" => "2022-05-02"
    "created_by" => 7
    "recipient_id" => 45
    "recipient_type" => "App\Models\Club"
    "recipient_name" => "Landskrona Pistolklubb"
    "recipient_address_street" => "null"
    "recipient_address_street_2" => "null"
    "recipient_address_zipcode" => "null"
    "recipient_address_city" => "null"
    "sender_id" => 73
    "sender_type" => "App\Models\Club"
    "sender_name" => "Eslövs Skyttegilles Pistolklubb"
    "sender_address_street" => "null"
    "sender_address_street_2" => "null"
    "sender_address_zipcode" => "null"
    "sender_address_city" => "null"
    "sender_bankgiro" => "null"
    "sender_postgiro" => "null"
    "sender_swish" => ""
    "amount" => "200.00"
    "payment_reference" => null
    "payment_gateway" => null
    "paid_at" => null
    "created_at" => "2022-04-18 21:51:46"
    "updated_at" => "2022-04-18 21:51:46"
    "deleted_at" => null
  ]
  #original: array:29 [
    "id" => 70
    "invoice_nr" => 1500
    "invoice_reference" => "7320221500"
    "expiration_date" => "2022-05-02"
    "created_by" => 7
    "recipient_id" => 45
    "recipient_type" => "App\Models\Club"
    "recipient_name" => "Landskrona Pistolklubb"
    "recipient_address_street" => "null"
    "recipient_address_street_2" => "null"
    "recipient_address_zipcode" => "null"
    "recipient_address_city" => "null"
    "sender_id" => 73
    "sender_type" => "App\Models\Club"
    "sender_name" => "Eslövs Skyttegilles Pistolklubb"
    "sender_address_street" => "null"
    "sender_address_street_2" => "null"
    "sender_address_zipcode" => "null"
    "sender_address_city" => "null"
    "sender_bankgiro" => "null"
    "sender_postgiro" => "null"
    "sender_swish" => ""
    "amount" => "200.00"
    "payment_reference" => null
    "payment_gateway" => null
    "paid_at" => null
    "created_at" => "2022-04-18 21:51:46"
    "updated_at" => "2022-04-18 21:51:46"
    "deleted_at" => null
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: array:1 [
    0 => "deleted_at"
  ]
  #dateFormat: null
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]
  #forceDeleting: false
}

But beside that another invoice is created as 1501 containing no amount.

Snapey's avatar

@raffelustig But you don't want an invoice number of 1500. you want the club_id, plus the year, plus the invoice number all concatenated into a single string.

raffelustig's avatar

@Snapey Yes, and I'm getting that: 7320221500 as you can see above. But I am also getting another invoice as the first but +1 and without amount. 7320221501. It seems to be created in the controller because nothing comes through the dump for the second empty invoice. And it sits in the database. There is also an invoice row table containing details about the invoice but not for the empty one.

raffelustig's avatar

This is the invoice records:

(76, 1500, '7320221500', '2022-05-02', 7, 45, 'App\Models\Club', 'Landskrona Pistolklubb', 'null', 'null', 'null', 'null', 73, 'App\Models\Club', 'Eslövs Skyttegilles Pistolklubb', 'null', 'null', 'null', 'null', 'null', 'null', '', '200.00', NULL, NULL, NULL, '2022-04-18 21:23:20', '2022-04-18 21:23:21', NULL),
(77, 1501, '7320221501', '2022-04-18', 7, 45, 'App\Models\Club', 'Landskrona Pistolklubb', 'null', 'null', 'null', 'null', 73, 'App\Models\Club', 'Eslövs Skyttegilles Pistolklubb', 'null', 'null', 'null', 'null', 'null', 'null', '', '0.00', NULL, NULL, NULL, '2022-04-18 21:23:21', '2022-04-18 21:23:21', NULL);

The invoice row:

(52, 76, 'Ralph Höglund fält 2022-05-08 (A1)', '1.00', 'st', '200.00', '0.00', '0.00', '200.00', '0.00', '200.00', 1, '2022-04-18 21:23:20', '2022-04-18 21:23:20', NULL);
raffelustig's avatar

This was for club 45 not 73. The competition organizer club is 73 and myself as signed up is in my club 45.

Snapey's avatar

What is the significance of '2022-04-18' since the second invoice has this date - its not a duplicate of the first record. This should give you a clue

raffelustig's avatar

@Snapey Yes, the first date '2022-05-02' for the real invoice is the payment due date. The second date I don't know where it comes from. Will look into the controller. Maybe I'll do some dumps there. I'm using beyondcode/laravel-dump-server. Its past midnight here so I continue tomorrow. Thanks a lot so far Snapey

raffelustig's avatar
raffelustig
OP
Best Answer
Level 2

The problem resolved with

  if($latestInvoiceNr == NULL):
        return 1500; 

So everything works now.

Please or to participate in this conversation.