I'm currently trying to generate a unique order number when the user reaches the create method. The order numbers are generated like this in the seed and need to look like this as well
Seed
foreach(range(1,25) as $index)
{
DB::table('orders')->insert([
'user_id' => rand(1,25),
'order_nr' => '#' . sprintf("%08d", $index),
'price_sum' => $faker->randomNumber($nbDigits = 4, $strict = false) . '.' . $faker->randomNumber($nbDigits = 2, $strict = false),
'status' => $faker->randomElement(['paid', 'pending', 'failed']),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
}
The order numbers look like this #00000001 or #00000002. Now when the user reaches the create method in my controller a new unique order number in this sequence needs to be created. How can I achieve that? The controller currently looks like this:
public function create()
{
$order = new Order;
$order->user_id = Auth()->id();
$order->order_nr =
dd($order);
return view('steps.order');
}
It needs to check the latest order number and create one with +1 on that order number. Say for instance there are 25 orders and the last one is #00000025 the one that needs to be created next needs to be #00000026. How can I achieve that?
Try doing as following
$order = new Order;
$order->user_id = Auth()->id();
$latestOrder = App\Order::orderBy('created_at','DESC')->first();
$order->order_nr = '#'.str_pad($latestOrder->id + 1, 8, "0", STR_PAD_LEFT);
$order->save();
Here I am assuming that the id is auto-incrementing. See the str_pad method for more details
Use the auto column id value of your rows to generate the order number. However, do not create an extra column for your order number, because this would give you an unnormalized DB, since the order-column is completely dependent of the id column.
Instead, add this method to you order model
public function get_order_number()
{
return '#' . str_pad($this->id, 8, "0", STR_PAD_LEFT);
}
If your last order had the id 5 and you would delete it, then the next order would have the id 6.
The only possible exception is if you create orders within an transaction. If a transaction is rolled back, the associated order id would be skipped.
You can use the Laravel ID generator.
First Install it:
composer require haruncpi/laravel-id-generator
Import the class in your controller.
use Haruncpi\LaravelIdGenerator\IdGenerator;
Now simply use it
$prefix = "#";
$id = IdGenerator::generate(['table' => 'your_table_name', 'length' => 9, 'prefix' =>$prefix]);
Output
#00000001
#00000002
#00000003
...
There is a problem in reading the maximum id and increment it.
In heavy servers or parallel requests, two requests may read the same max(id) from MySQL.
The best way to create unique sequence number is use a sequence table with just one auto increment field.
First insert new record in the sequence table.
Then read the last inserted id from db by LAST_INSERT_ID() MySQL function.
At last you can remove old records lower than your id.
Laravel way:
$id = DB::table('seq_foo')->insertGetId(['id' => null]);
DB::table('seq_foo')->where('id', '<', $id)->delete();
$id is your unique sequence number.
You may try with this:
public function generateOrderNR()
{
$orderObj = \DB::table('orders')->select('order_nr')->latest('id')->first();
if ($orderObj) {
$orderNr = $orderObj->order_nr;
$removed1char = substr($orderNr, 1);
$generateOrder_nr = $stpad = '#' . str_pad($removed1char + 1, 8, "0", STR_PAD_LEFT);
} else {
$generateOrder_nr = '#' . str_pad(1, 8, "0", STR_PAD_LEFT);
}
return $generateOrder_nr;
}
You can generate order_nr by using this: $this->generateOrderNR(); in your create() function.
In addition mt_rand() is 4 times faster than rand() you may use this for better user experience.
I have found a better solution:
$paynowbillprefix1="ADDON-";
$paynowbillprefix1=strlen($paynowbillprefix1);
$query2 = "select gen_id from user_cart_addon order by id desc limit 0, 1";
$exec2 = mysqli_query($conn,$query2) or die ("Error in Query2".mysqli_error());
$res2 = mysqli_fetch_array($exec2);
$num2 = mysqli_num_rows($exec2);
$billnumber = $res2["gen_id"];
$billdigit=strlen($billnumber);
if ($billnumber == '')
{
$billnumbercode ='ADDON-'.'1';
}
else
{
$billnumber = $res2["gen_id"];
$billnumbercode = substr($billnumber,$paynowbillprefix1, $billdigit);
$billnumbercode = intval($billnumbercode);
$billnumbercode = $billnumbercode + 1;
$maxanum = $billnumbercode;
$billnumbercode = 'ADDON-'.$maxanum;
}
Related
So Similar to the unique with validation rule (See: https://github.com/felixkiss/uniquewith-validator), I want to know how to generate a entry, where one column is unique with another one. I want to seed my database as follows.
Example:
There are 12 steps in the "steps" table. Each step should have 5 categories associated with each one that are stored in the "step_categories" table. Each of those categories are assigned a unique order number 1 through 5 that is unique with each "step_id".
See this image here for an example of what the database should look like: https://imgur.com/a/XYA5yyn
I had to manually to make the entries in the database for the above image example. I don't want to have to generate this manually every time, say I make a mistake and have to rollback the migrations for example.
I am using a factory to generate this data. So the factory name is StepCategoriesFactory.php and clearly I'm calling the factory with the create() method from the DatabaseSeeder.php file.
I thought about doing this in a for loop, then i got as far as realizing when i called the 'step_id' => App\Model::all()->random()->id to grab a new id, that I wouldn't be able to ensure I wasn't grabbing the id that i just generated 5 entries for. I'm really new with Laravel, and I'm not sure where to even start on this. There's no real information on SO where faker can use the unique with another column. How would I Go about this?
NOTE: The step id is not always going to be 1-12. The step ID might be different depending on whether a step gets deleted and remade. So just assigning the step_id to equal 1-12 wont work.
UPDATE: Here's some code I just wrote, and I think I'm on the right track. Maybe. I've grabbed the step_id by it's number field as that will always be 1-12, and I've grabbed the IID out of the entry. But now I'm stuck on how to generate the order 1-5 without repeating itself. I still haven't run this yet as its incomplete and I know it'll throw an error without the correct order number.
UPDATE 2: I think I'm on the right track here. However I'm getting an undefined variable error. When I put the first line from within the anonymous function, it's resetting the order to "1" for every entry. How do i make the $autoIncrement variable available to the anonymous function? The Seeder has stayed the same between updates.
Image of the error: https://imgur.com/a/ywkd0Lb
Second image with the Die/Dump error in terminal: https://imgur.com/a/rOGRv32
Reference this article here: https://laracasts.com/discuss/channels/laravel/model-factory-increment-value-faker?page=1
UPDATE 3: I forgot the use ($autoIncrement) line of code for the anonymous function. Code below has been updated, but now I'm getting a different error saying that the order column has a null value and can't be inserted. clearly it should be '1'. Even after I call my $autoIncrement->next(); which should increment it to '1' it's still returning null according to the terminal. However, when I do a diedump on $autoIncrement->current() it's returning 1. Weird.
Update 3 error: https://imgur.com/a/STOmIjF
StepCategoriesFactory.php
use Faker\Generator as Faker;
$autoIncrement = autoIncrement();
$factory->define(App\StepCategory::class, function (Faker $faker) use ($autoIncrement) {
// Generate Created At and Updated at DATETIME
$DateTime = $faker->dateTime($max = 'now');
$autoIncrement->next();
$order = (int) $autoIncrement->current();
return [
// Generate Dummy Data
'order' => $order,
'name' => $faker->words(4, true),
'created_at' => $DateTime,
'updated_at' => $DateTime,
];
});
function autoIncrement()
{
for ($i = 0; $i < 5; $i++) {
yield $i;
}
}
Edit: Put a bounty on this question, as I think it would be helpful for the community to get a detailed answer. I'm looking for help to explain how to go about making sure I'm grabbing the same entry through each loop.
FINALLY SOLVED!
So I took in everyone's answers, and thought long and hard about using a for loop to create the order number. 1-5. The problem that I was running into at the end was that the $i variable was not resetting. So after the yield I had to check if the $i variable equalled 5 and then reset it back to zero.
Heres the code!
StepCategories.php
use Faker\Generator as Faker;
$autoIncrement = autoIncrement();
$factory->define(App\StepCategory::class, function (Faker $faker) use ($autoIncrement) {
// Generate Created At and Updated at DATETIME
$DateTime = $faker->dateTime($max = 'now');
// Get the next iteration of the autoIncrement Function
$autoIncrement->next();
// Assign the current $i value to a typecast variable.
$order = (int) $autoIncrement->current();
return [
// Generate Dummy Data
'order' => $order,
'name' => $faker->words(4, true),
'created_at' => $DateTime,
'updated_at' => $DateTime,
];
});
function autoIncrement()
{
// Start a loop
for ($i = 0; $i <= 5; $i++) {
// Yield the current value of $i
yield $i;
// If $i is equal to 5, that must mean the start of a new loop
if($i == 5) {
// Reset $i to 0 to start over.
$i = 0;
}
}
}
DatabaseSeeder.php
// Generate Dummy Categories
// Run the factory 12 times
foreach(range(1, 12) as $i) {
// Generate 5 entries each time
factory(App\StepCategory::class, 5)->create([
// Since all steps have a number 1-12 grab the step by the number column and get it's ID
'step_id' => App\Step::where('number', '=', $i)->first()->id,
]);
}
Thanks to all who helped!
Sorry if you don't understand my point so I'll try to explain it in code
use Illuminate\Database\Seeder;
$factory->define(App\StepCategory::class, function (Faker $faker) {
// Generate Created At and Updated at DATETIME
$DateTime = $faker->dateTime($max = 'now');
$step_id = function () {
return factory('App\Step')->create()->id;
};
return [
// Generate Dummy Data
'step_id' => $step_id,
'order' => uniqueOrder($step_id),
'name' => $faker->words(4, true),
'created_at' => $DateTime,
'updated_at' => $DateTime,
];
});
function uniqueOrder($step_id)
{
$unique = rand(1,5);
do {
$unique = rand(1,5);
}
while(StepCategory::where('step_id', $step_id)->andWhere( 'order', $unique)->exists())
return $unique;
}
for example if your Step model name is Steps :
$allSteps = Steps::all();
foreach($allSteps as $step){
for($i=1;$i<6;$i++){
//insert to table $step->id , $i for example
DB::table('yourTableName')->insert([
'name'=>'Step '.$step->id.'- Category '.$i ,
'order'=>$i ,
'step_id'=>$step->id
]);
}
}
I used to group on mongoDB via PHP to get the max date of my items.
As i have too many items (more than 10 000), i read i must use MapReduce.
Here's my past group function :
$keys = array('ItemDate'=> true);
$initial = array('myLastDate' => 0);
$reduce = "function(obj, prev) {
if (myLastDate < obj.ItemDate) {
myLastDate = ItemDate;
}
}";
$conds = array( 'ItemID' => (int) $id );
$results = $db->items->group($keys, $initial, $reduce,
array('condition'=> $conds ) );
I've tried something but seems not to work ...
$map = new MongoCode("function() {
emit(this.ItemID,this.ItemDate);
}");
$reduce = new MongoCode("function(obj, prev) {
if(prev.myLastDate < obj.ItemDate) {
prev.myLastDate = obj.ItemDate;
}
}");
$items = $db->command(array(
"mapreduce" => "items",
"map" => $map,
"reduce" => $reduce,
"query" => array("ItemID" => $id);
$results = $db->selectCollection($items['result'])->find();
Can you please help ?
Solution
You don't need to use map/reduce for that. Provided your date field contains an ISODate, a simple query does the trick:
db.yourColl.find({},{_id:0,ItemDate:1}).sort({ItemDate:-1}).limit(1)
In order to have this query done efficiently, you need to set an index on ItemDate
db.yourColl.createIndex({ItemDate:-1})
Explanation
The query
Let us dissect the query. db.yourColl...
.find({} The default query
,{_id:0,ItemDate:1} We want only ItemDate to be returned. This is called a projection.
.sort({ItemDate:-1}) The documents returned should be sorted in descending order on ItemDate, making the document with the newest date the first to be returned.
.limit(1) And since we only want the newest, we limit the result set to it.
The index
We create the index in descending order, since this is the way you are going to use it. However, if you need to change the default query to something else, the index you create should include all fields you inspect in the query, in the exact order.
I have a unique constraint in my database for two columns.
$table->unique(array("table_a_id", "table_b_id"));
Now I use Faker in a seeder class to fill in this pivot table:
$tableAIds = TableA::all()->lists("id")->toArray();
$tableBIds = TableB::all()->lists("id")->toArray();
foreach(range(1, 20) as $index) {
$tableAId = $faker->randomElement($tableAIds);
$tableBId = $faker->randomElement($tableBIds);
DB::table("table_a_table_b_pivot")->insert([
"table_a_id" => $tableAId,
"table_b_id" => $tableBId
]);
}
However this creates duplicates and the seeding fails due to SQL error.
How do I make sure I don't try to insert duplicates?
You can use this:
$tableAIds = TableA::all()->lists("id")->toArray();
$tableBIds = TableB::all()->lists("id")->toArray();
foreach (range(1, 20) as $index) {
repeat:
$tableAId = $faker->randomElement($tableAIds);
$tableBId = $faker->randomElement($tableBIds);
try {
DB::table("table_a_table_b_pivot")->insert([
"table_a_id" => $tableAId,
"table_b_id" => $tableBId,
]);
} catch (\Illuminate\Database\QueryException $e) {
//look for integrity violation exception (23000)
if($e->errorInfo[0]==23000)
goto repeat;
}
}
Ps: This may not work if from the begining TableA and TableB datas are inconsistents (meaning it is not possible to form 20 uniques couples from those data )
$unique = $faker->unique()->regexify('[0-4][0-9][0-4][0-9]');
The code above will result in a string composed of two numbers between 0 and 49 that never repeats. Now you can break into two and seed your database securely.
$firstId = ltrim(substr($unique, 0, 2), '0') + 1;
$secondId = ltrim(substr($unique, 2, 2), '0') + 1;
The two numbers can be the same in a string, but a string will always be unique. Don't forget to add one for each of the parts because we don't want (and we can't) to seed our database with zeros.
Instead of using faker, use the collection's random method:
$tableAIds = TableA::all()->lists('id')->random(20);
$tableBIds = TableB::all()->lists('id')->random(20);
foreach(range(0, 19) as $index) {
DB::table("table_a_table_b_pivot")->insert([
"table_a_id" => $tableAIds[$index],
"table_b_id" => $tableBIds[$index],
]);
}
Adjust this according to your needs.
For example, this might be what you really want:
$tableAIds = TableA::all()->lists('id');
$tableBIds = TableB::all()->lists('id');
foreach ($tableAIds->random(5) as $tableAId) {
foreach ($tableBIds->random(2) as $tableBId) {
DB::table("table_a_table_b_pivot")->insert([
"table_a_id" => $tableAId,
"table_b_id" => $tableBId,
]);
}
}
Whatever it is, play with it yourself. No need for faker here.
I am using ci and using its active record pattern to access database. I want to update table using statement like
UPDATE employee_barcode set `count` = `count` + 1
where barcode_id = 2
I tried using update statement like this
$data = array(
'count' => 'count' + 1,
);
$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode', $data);
But the result was wrong.
How can i do so?
This doesn't work because $this->db->update can't get the value of count, and therefore can't add 1 to it. You should get the count value using $this->db->select, then proceed to update the value.
For example:
$this->db->where('barcode_id', 2);
$this->db->select('count');
$query = $this->db->get('employee_barcode');
$row = $query->row();
$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode', array('count' => ($row->count + 1)));
Try this..
$this->db->set('count', '`count+1`', FALSE)
$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode');
I'm trying to create 10 new rows in controller if the userid is not found in the user_id row. I tried create() function and then save() function but it doesn't seem to do the job.
Below is the code, is there a way we can solve this issue?
function invite_fellows(){
//Read userid
$userid = $this->Session->read('Auth.User.id');
$invite_table = $this->User->Invite->findbyUserId($userid);
if(empty($invite_table)){
$code_limit = 10;
//Save 10 unique codes for the user
for($i=0; $i<$code_limit;$i++){
$unique_id = $this->_unique($userid); // Unique id with userid as initial prefix
$this->data['Invite'] = array('user_id' => $userid, 'code' => $unique_id);
$this->User->Invite->create();
$this->User->Invite->save($this->data['Invite']);
}
}
//Find user in users and associated tables
$user = $this->User->findbyId($userid);
//Find user in invite table
$confirmed = $this->User->Invite->find('count', array('conditions' => array('user_id' => $userid,'invited_user >' => 0)));
$this->set(compact('user','confirmed'));
}
Thank you.
Most likely there's a validation rule that blocks the save. Try adding debug( $this->User->Invite->validationErrors ); after the save() to check for that. Make sure debug level is set to at least 1 in core.php.