Skip to main content

Object of class App\Invoice could not be converted to int

 In my case I was doing this:

<?php
$getallinvoiceids = \App\Invoice::all();

echo 'in'.'-'.sprintf('%06d',$getallinvoiceids->last()->id+1);

?>

So, $getallinvoiceids->last()->id+1 was giving me the error of App\Invoice could not be converted to int. as it was searching for the last data but the invoices table was empty. 


Now to solve this, first we need to count the table then do the operation. Like:

<?php 
     $getallinvoiceids = \App\Invoice::all();
     if($getallinvoiceids->count() <= 0)
     {
        $rowcount = 1;
     }
     else
     {
        $rowcount = $getallinvoiceids->last()->id+1;
     }
     echo 'in'.'-'.sprintf('%06d',($rowcount));
?>


Thanks.


Comments

Popular posts from this blog