Adding values in database with query in laravel - laravel

I want to add 10 from a current database value.
Current db value = 20;adding = 10;updated value 30;
The following code is not working.
DB::table('employee')->increment('bonus'=>'bonus+10');

Try this:
DB::table('employee')->increment('bonus', 10);

Try this -
DB::table('employee')->increment('bonus', 10);
You can do like this as well:
DB::table('employee')
->where('rowID', 1) // if you to add in a perticular table else comment it for updating entire column's value by adding 10 in it.
->update([
'bonus' => DB::raw('bonus + 10'),
]);
Hope this will help you.

Related

How to update a single value with laravel

Hello i want to update a single value on another table using laravel. This is the code i have done until now but doesnt seem to work:
$amount = Product::findorFail($request->products[$i]);
$total_value = $request->amount[$i] + $amount->amount;
$amount->update(['amount', $total_value]);
dd($total_value);
with dd i see that the result is correct but the update function is not, the query im trying to make is
update table set amount=x where id=y
You have multiple choices. The shortes are:
$amount->update(['amount'=> $amount->amount + request->amount[$i]]);
or
Product::findorFail($request->products[$i])->increment('amount', $request->amount[$i]);
you could change your code like below
$amount = Product::findorFail($request->products[$i]);
$total_value = $request->amount[$i] + $amount->amount;
$amount->amount=$total_value;
$amount->update();
or as mentioned in comments you could use eloquent increment function

Codeigniter updating data with OR condition in ID

Hello I am a beginner in using Codeigniter. What I want is to update a data in Codeigniter with an OR condition to the ID.
Here is my MySQL query that I want to make happen:
Update message_received SET is_read = 1 WHERE msg_id = 3 OR parent_id = 3
I tried something like this:
$this->query->update_array('message_received', array('msg_id' => $_POST['read_my_message'],'parent_id' => $_POST['read_my_message']) , array('is_read'=>1));
You can use or_where to apply OR condition in CodeIgniter, I've written a possible solution for your question(Reference).
See if it helps you.
$msg = $this->input->post('read_my_message'); // get the post data
$this->db->set('is_read', 1); // set the value of column
$this->db->where('msg_id', $msg); // first condition
$this->db->or_where('parent_id', $msg); // second contion
$this->db->update('message_received'); // table name
// Produces:
/* UPDATE `message_received` SET `is_read` = 1 WHERE `msg_id` = '$msg' OR `parent_id` = '$msg' */
You can apply OR. The references https://www.codeigniter.com/userguide3/database/query_builder.html

What is the equavelent of mysql : SET in laravel 4.2?

i am new to Laravel and now trying to use transaction in Laravel 4.2
I am trying to replace this Mysql query in laravel.
UPDATE managerStock SET amount = amount + $productAmount WHERE manager_id = '$receiverID' and product_id='$productID
So far i came up with this code
DB::table('distributorStock')->where('distributor_id','=','senderID')->where('manager_id','=','receiverID')->update(array('amount' => ''));
I am looking for your kind help regarding this challenge!
Thanks
if you want to replace bellow mysql query -
UPDATE managerStock SET amount = amount + $productAmount WHERE manager_id = '$receiverID' and product_id='$productID'
Possible answer could be -
$dbData = DB::table('managerStock')->where('manager_id', $receiverID)->where('product_id', $productID)->first();
$dbData->amount = $dbData->amount+$productAmount;
$dbData->save();
for more than one database query as a transaction you can use -
DB::transaction(function() use ($dbData1, $dbData2, $dbData3) {
$dbData1->save();
$dbData2->save();
$dbData3->save();
});

Doctrine ORM Update Row with ID

Is there a way to update a row directly with and ID? I just want the ability to update a table row field without querying for the object first. I tried this...
$id = 1;
$s = new Sandbox();
$s->setId($id);
$s->setFname('moon');
$e = $em->merge($s);
$em->flush($e);
and it tried to do an update to the database, however, it failed because it tried to update all the undefined fields as well whereas I just want to update the fname field.
Thanks
$id = 1;
$s = $em->getReference('Sandbox', $id);
$s->setFname('moon');
$em->persist($s);
$em->flush($s);

code igniter updating a row with multiple values to a column

I am getting a array to my model. Say array has an value 1 and 2.
Now I need to update this to a single column 1,2 so that column will look like this
Column
1,2
foreach($x as $y){
$this->db->where('column',4);
$this->db->set('id',$data);
$this->db->update('table');
}
But this is only update 2, it is omitting 1. Where am i going wrong ?
If you want to save multiple values in a column put them in
an array and serialize them. Than save it in column.
Make the column type text.
$array['name'] = 'test';
$array['otherinfo'] = 'otherinfo';
$data = serialize($array);
foreach($x as $y)
{
$this->db->where('column',4);
$this->db->set('id',$data);
$this->db->update('table');
}
When you select you can unserialize the array using unserialize();
Use implode
For example:
$data = [1,2]
$comma_data = implode(",", $data)
Then update column with $comma_data
I was having wrong data type, I figured it out.

Resources