Skip to content

Instantly share code, notes, and snippets.

@ivandez
Created May 6, 2022 12:56
Show Gist options
  • Save ivandez/ce737459f2d67cbe5c436195b5a8d862 to your computer and use it in GitHub Desktop.
Save ivandez/ce737459f2d67cbe5c436195b5a8d862 to your computer and use it in GitHub Desktop.
contar productos
public static function getSerialNumber()
{
// Get the last created order
$lastOrder = Product::orderBy('created_at', 'desc')->first();
if (!$lastOrder)
// We get here if there is no order at all
// If there is no number set it to 0, which will be 1 at the end.
$number = 0;
else
$number = substr($lastOrder->serial_number, 3);
// If we have SOL0000001 in the database then we only want the number
// So the substr returns this 0000001
// Add the string in front and higher up the number.
// the %07d part makes sure that there are always 7 numbers in the string.
// so it adds the missing zero's when needed.
return 'P' . sprintf('%07d', intval($number) + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment