How to update all column with 0 value to specific value? - laravel

I want to update all column with 0 value to 1
Controller
$totalofitem=assets::where('item_name','like','keyboard')
->where('total_cost','like','0')->update(array('total_cost' => '1'));
Got it
$totalofitem=assets::where('item_name','like','keyboard')
->where('total_cost','like','0')->update(['total_cost' => 1]);

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,
]);

How to update multiple rows sharing same foreign key in Codeigniter

I have 2 tables; db_file_acess and db_file_access_details. I have a column subjects_id in db_file_access_details table. From the UI, I can select several subjects[array] with other details and insert them to db_file_acess table, and subject_ids to the db_file_access_details. Insertion is OK but when I update the two tables, I can't seem to update all the rows. This is my update query
$result = array();
foreach ($subject_id as $key => $val) {
$result[] = array(
'file_access_id' =>$q_id,
'subjects_id' => $_POST['subject_id'][$key]
);
}
$this->db->update_batch('db_file_access_details', $result, 'file_access_id');
The query runs but it updates with only one id as seen in the screenshot below:
the result() array is as shared below:
Array
(
[0] => Array
(
[file_access_id] => 45
[subjects_id] => 1
)
[1] => Array
(
[file_access_id] => 45
[subjects_id] => 3
)
[2] => Array
(
[file_access_id] => 45
[subjects_id] => 4
)
)
after update_batch query, the resultant results is as shown below
What am I doing wrong?
According to the docs update_batch('db_file_access_details', $result, 'file_access_id') produces:
UPDATE `db_file_access_details` SET `subjects_id` = CASE
WHEN `file_access_id` = 45 THEN 1
WHEN `file_access_id` = 45 THEN 3
WHEN `file_access_id` = 45 THEN 4
ELSE `subjects_id` END
WHERE `file_access_id` IN (45, 45, 45)
For all three rows, the first case matches, so all subject_ids are set to 1.
For update_batch to work correctly you need to use the id column.
Depending on what you want to achieve, you could also consider deleting all rows for the file_access_id in db_file_access_details and then inserting the $result array with insert_batch instead.

how to use increment function in 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,
]);

How do I output the diffence between values of Array A to Array B in Laravel?

I have an array that plucks values to an array like so:
/* Pluck just the wattage values to an array */
$realtime_data_array = $latestrtfeed->nth(60)->reverse()->pluck('data')->toArray();
which results in:
array:150 [▼
0 => 20277.6
1 => 20281.4
2 => 20285.3
3 => 20289.7
4 => 20293.8
5 => 20298.6
6 => 20303.2
7 => 20307.4
8 => 20311.5
9 => 20315.8
10 => 20319.8
these value get inputted to the chart like so:
$realtime_consumption_chart->dataset('kWh', 'line', $realtime_data_array);
The problem is that the sensor is storing cumulative values and I only want to show the difference between value 0 and value 1 then difference between value 1 and value 2 and so on and so on.
How would I go about something like that?
I am going to take a shot and say that I'm going to have to do a FOREACH on the array and create a new array with the values adjusted, but how do I go about that? maybe:
foreach ($realtime_data_array as $data) {
$realtime_data_array_corrected = ($data[1]-$data[0])->toArray();
}
Nope there is something I'm doing wrong or not considering here.
for ($i = 0; $i < sizeof($realtime_data_array); $i++) {
if($i==0) {
$realtime_data_array_corrected[$i] = 0;
continue;
}
$realtime_data_array_corrected[$i] = $realtime_data_array[$i] - $realtime_data_array[$i-1];
}

CodeIgniter - Insert a new row in db with a boolean value

When CodeIgniter is inserting a row into a DB, it doesn't encode PHP booleans into a form MySQL needs.
For example:
$new_record = array(
"name" => "Don",
"is_awesome" => true
);
This will enter into MySQL as this:
name (varchar) is_awesome (tinyint)
Don 0
Anyone know a good way to deal with this? I've been writing (is_awesome == true) ? 1 : 0; then setting the array value, but that sucks.
you can't add true or false to a TINYINT in mysql. you should do 1 or 0 like this
$new_record = array(
"name" => "Don",
"is_awesome" => 1 //1 means it's true
);
$query = $this->db->insert('table_name', $new_record);
then just when you fetch it consider 0 as false and 1 as true
Update:
you can create a function called tinyint_decode like this:
public function tinyint_decode($result = array(), $decode_set = array())
{
//$result is what you got from database after selecting
//$decode_set is what you would like to replace 0 and 1 for
//$decode_set can be like array(0=>'active', 1=>'inactive')
//after fetching the array
$result->is_awesome = ($result->is_awesome == 1 ? $decode_set[1] : $decode_set[0]);
return true;// or anything else
}
this way you can interpret 0 and 1 by anything you like whether it's true and false, active and inactive, or anything else just by passing $decode_set array.
MySQL doesn't have boolean values. What I usually do is:
1) Have a field of length char(1) and say 'Y' or 'N'
OR
2) Have a numeric field and say '1' or '0'
The one method to encode you mentioned is the only way to do it. If it's an extra step, I would just get rid of boolean in the PHP code itself and make it a '1-0' or 'Y-N'

Resources