Generate a Number

Published 5 days ago by Laravelephant

What would you say is the best way to do this?

I am taking a number from a variable $job and I want to create a new variable ($newJob) which is $job plus an increment. This may have happened in the past so I need to be able to ensure that I'm always going one number higher.

For example:

$job = 1234. In the first pass I would want to be $newJob = 12341.

In the second then $newJob = 12342 and so on.

Each of these variables is getting stored in the DB.

Any suggestions?

Best Answer (As Selected By Laravelephant)
36864

I had this open for a while before replying so I didn't see your first reply before posting.

To make sure I'm understanding your problem clearly:

You have a model called Orders which has a column po_number which should be equal to the job column, concatenated with an incremental number in order to create unique po_number values. Assuming job is a unique identifier, then job . increment would also be a unique po_number identifier.

This can be done simply by appending a 1 to the job number for the first po_number, and making every po_number after that 1 plus the previous po_number.

public function newPoNumber($job)
{
    $order = Order::where('job', $job)->orderBy('po_number', 'desc')->first;
    if($order) {
        return $order->po_number + 1;
    }
    else {
        return $job . 1;
    }   
}
topvillas
topvillas
5 days ago (131,335 XP)

Not sure what you're asking ...

$newJob = $job + $increment;
Laravelephant

Sorry, I'm probably being a bit unclear. So basically I've got a DB with a bunch of existing values. Each row has a job field and a newJob field.

When a new row is entered, I want to ensure that newJob is always one higher than any previous instance. So if we had the base number of 1234 then there might be several rows with job as 1234 and their newJob field would be 12341, 12342, 12343 and so on.

I've written a small function like this for instance:

 public function generatePONumber($jobNumber)
    {
        $base = Order::where('job_number', $jobNumber)->orderBy('po_number')->get();
        if(count($base)) {
            if($base->job_number) {
                $jobNumber = $base->jobNumber + 1;
            } 
            else {
                $jobNumber = $base + 1;
            }
            return $jobNumber;
        }
    }
36864
36864
5 days ago (29,390 XP)

Each of these variables is getting stored in the DB.

I don't know how you're doing this, so have a generic solution:

public function createNewJob($job, $increment) {
    return $job . $increment;
}

Just increment the value of $increment after calling that and save it to the database.

Laravelephant

Hey @36864

Thanks for this. It's along the lines of what I was wanting to do but the issue is that I don't know what the increment needs to be. There are two columns: job and po_number.

The po_number is always going to increment, but when creating a new entry, I only have the job number.

So I need to find the latest po_number based on the job_number and then increment by one.

kobear
kobear
4 days ago (11,010 XP)

What DB platform are you storing the number in? In MySQL you can set a field to be auto-incrementing and set a custom starting point for that field.

https://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html

martinbean

@Laravelephant 12341 is not “one number higher” than 1234.

Laravelephant

@martinbean - whoops! I meant I need to add a number onto the end and increment that last digit. Fair point though.

@kobear - Thanks this is pretty useful to know but I think that I'm still stuck with the same issue in that I need to know the latest po_number to increment from.

36864
36864
4 days ago (29,390 XP)

I had this open for a while before replying so I didn't see your first reply before posting.

To make sure I'm understanding your problem clearly:

You have a model called Orders which has a column po_number which should be equal to the job column, concatenated with an incremental number in order to create unique po_number values. Assuming job is a unique identifier, then job . increment would also be a unique po_number identifier.

This can be done simply by appending a 1 to the job number for the first po_number, and making every po_number after that 1 plus the previous po_number.

public function newPoNumber($job)
{
    $order = Order::where('job', $job)->orderBy('po_number', 'desc')->first;
    if($order) {
        return $order->po_number + 1;
    }
    else {
        return $job . 1;
    }   
}
Laravelephant

Hey @36864

That worked perfectly! It seems really obvious with hindsight. Thanks!

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