how to use increment function in laravel - laravel

i am using DB to store values in database.
i have "course fees" column i what to "increment" the "course_fees" value in column.
for example
DB::table('student')->where('registration_id','=', $request->registration_id)->increment(['course_fees' =>$request->course_fees]);
this code increment the inserted value
how can i modified below code for increment "course_fees" value like above
DB::table('student')->where('registration_id','=', $request->registration_id)->update(['payment_date' => $request->payment_date,'balance_fees' => $request->balance_fees,'course_fees' =>$request->course_fees]);

You cannot use this method to increment multiple fields. You can use:
$studentQuery = DB::table('student')->where('registration_id','=', $request->registration_id);
(clone $studentQuery)->increment('payment_date',$request->payment_date);
(clone $studentQuery)->increment('balance_fees', $request->balance_fees);
(clone $studentQuery)->increment('course_fees', $request->course_fees);
but this way you will run 3 database queries to update.
But if you are sure there is exactly single record found for registration_id you can do it like this:
$student = DB::table('student')->where('registration_id','=', $request->registration_id)->first();
$student->update([
'payment_date' => $student->payment_date + $request->payment_date,
'balance_fees' => $student->balance_fees + $request->balance_fees,
'course_fees' => $student->course_fees + $request->course_fees
]);
EDIT
If you want to increment only course_fees column and want to update other 2 columns from input you can use:
DB::table('student')->where('registration_id','=', $request->registration_id)
->increment('course_fees' , $request->course_fees, [
'payment_date' => $request->payment_date,
'balance_fees' => $request->balance_fees
])
This is documentation about increment/decrement methods.

increment()/decrement() can take 3 parameters: $column, $amount, $extra.
$column is the field that you want to increment
$amount is by how much you want to increment the field by
$extra is an array of attributes that you also want to update in the query.
If you don't pass an amount the default for $amount is 1.
To achieve what you're after you could do:
DB::table('student')
->where('registration_id', $request->registration_id)
->increment('course_fees', $request->course_fees, [
'payment_date' => $request->payment_date,
'balance_fees' => $request->balance_fees,
]);

Related

increment or decrement without using laravel methods mangodb jassengers

I want to update documents and decrement specific column value. it is possible to use Model::where('','')->update(['count' => \DB::raw('count- 1')]); i'm using jenssegers/laravel-mongodb package.
I want decrement and update documents values together.
$result= Source::where('')
->where('');
$result->decrement('count');
$result->update([
'column' => true,
]);
This code decrement count value but can not update column values.
From the docs:
Perform increments or decrements (default 1) on specified attributes:
Cat::where('name', 'Kitty')->increment('age');
Car::where('name', 'Toyota')->decrement('weight', 50);
i found it from laravel doc
updated column value pass 3rd parameter as array in decrement method
Model::where('', null)
->where('', '')
->decrement('count', 1, [
'column1' => value1,
'column2' => value2,
'column3' => value3,
]);

Laravel - what does the manual paginations 'option' query parameter do?

The manual pagination I found while googling works fine but I was just wondering what does the 'query' => $request->query() in the option parameter does?
$total = count($form_list);
$per_page = 10;
$current_page = $request->input('page') ?? 1;
$starting_point = ($current_page * $per_page) - $per_page;
$form_list = array_slice($form_list, $starting_point, $per_page, true);
$form_list = new Paginator($form_list, $total, $per_page, $current_page, [
'path' => $request->url(),
'query' => $request->query(),
]);
Calling ->query() without any parameters returns all the values from the query string as an associative array.
Suppose you have a query string like this:
https://example.com/path/to/page?name=ferret&color=purple
You can retrieve the value of name by doing something like so:
$request->query('name')
which returns ferret. You can also pass a second parameter for a default value so if you call:
$request->query('size', 'Medium')
which doesn't exist on the query string, you'll get 'Medium' instead of null.
If you omit all parameters, you'll receive an associative array that looks something like this:
query = [
'name' => 'ferret',
'color' => 'purple',
]
The options parameter is not needed by the pagination itself but for your dataset query. If you do not pass the query parameter, when you click one of the pagination urls, you'll get something like this:
https://example.com/path/to/page?page=2&per_page=5
Sometimes, this works fine and will give us something that we want but sometimes, we need those additional query string to get the correct dataset. We pass in all values from our query to get something like this:
https://example.com/path/to/page?page=2&per_page=5&name=ferret&color=purple
Which will filter your dataset for all those purple ferrets. As for the question if you need it, it's up for you to decide if that is essential for your code or if you can get away with just pagination.
Good luck! I hope this helps.

how to Run SQL insert Using Multiple Database Connections

this my code
DB::connection('mysql2')->insert('INSERT INTO pm_booking_service (id_booking, title, qty, amount) VALUES ('2','restaurant','1','27')' );
I don't know how to do insert Using Multiple Database Connections
but select is working fine
$bookings = DB::connection('mysql2')->select('select * from pm_booking');
When you have a string that starts with a single quote you need to escape any single quotes it contains otherwise they are interpreted as closing the string.
Use parameters to not need to quote the values manually:
DB::connection('mysql2')
->insert('INSERT INTO pm_booking_service (id_booking, title, qty, amount) VALUES (?,?,?,?)', [2,'restaurant',1,27]);
An example of this can be found in the docs as well
This can be rewritten in the query builder as :
DB::connection('mysql2')->table('pm_booking_service')
->insert([
'id_booking' => 2,
'title' => 'restaurant',
'qty' => 1,
'amount' => 27
]);
Update statements are also written similarly:
DB::update(
'update pm_booking_service set qty = ? where id = ?',
[100, 2]
);
This also can be written in the query builder as:
DB::connection('mysql2')->table('pm_booking_service')
->where('id', 2)
->update([ 'qty' => 100 ]);

Row inserted but number not

I trying to insert a row in codeigniter and row inserted.
Problem is all row inserted properly but in sql bighint(11) field inserted 0.
I checked properly in array value given.
$data = [
'sku' => $POST['rec1'],
'pruch_price' => $POST['rec2'],
'sell_price' => $POST['rec3']
];
$model->insert ($data);
you should use $_POST[] instead of $POST.
But better yet, don't send $_POST directly to the models, instead use the post request provided by Codeigniter 4.
$data = [
'sku' => $this->request->getPost('rec1'),
'pruch_price' => $this->request->getPost('rec2'),
'sell_price' => $this->request->getPost('rec3')
];
$model->insert($data);

updateOrCreate with increment in laravel

What I am trying to do is to update or insert the row in table. In my case, update looks something like this:
\DB::table('inventories')->where('product_code',$product_code)->increment('stock_current_quantity',$quantity);
I don't want to use if else statement. What I actually want is to integrate increment into following statement so it update as above statement.
\App\Inventory::updateOrCreate(['product_code' => $product_code], ['stock_current_quantity'=>$quantity]);
Thanks in advance!
Because google brought me here and I think the answers here, especially when you simply want to use updateOrCreate, are not as satisfying as this:
\App\Inventory::updateOrCreate([
'product_code' => $product_code
],
[
'stock_current_quantity' => \DB::raw('stock_current_quantity + 1')
]
);
Credits to this guy
Why not do:
$inventory = \App\Inventory::firstOrNew(['product_code' => $product_code]);
$inventory->stock_current_quantity = ($inventory->stock_current_quantity + $quantity);
$inventory->save();
If the model doesn't exists, $inventory->stock_current_quantity will only be equal to $quantity, else, it will increment it by $quantity.
I am using Laravel 7.8.1 and rolling it all into one statement as per the following works fine:
$user->league_entries()->updateOrCreate([
'month' => $month,
'year' => $year
])->increment('score');
just contributing if someone else has this problem
it doesn't exist, creates, but with zero
self::firstOrNew(['product_code' => $product_code]);
then, increment it... if other process updates it to 1 before me (in this middle time between firstOrNew and increment), this will update to 2, avoiding losing the prior update
self::where('product_code', $product_code)->increment('stock_current_quantity');
Similar to Inigo's answer, but slight change i would go with:
$user->league_entries()->firstOrCreate([
'month' => $month,
'year' => $year
])->increment('score');

Resources