increment or decrement without using laravel methods mangodb jassengers - laravel

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

Related

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

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

Any way to except empty field from $request->all() in laravel?

I want to except empty value field from $request->all();
Array ( [first_name] => Dev 1 [password] => [last_name] => [phone] => 123456 [password_confirmation] => )
I got the array like this but I want to except field from above array like last_name, password which has no value.
Any way to do this without for loop.
I mean laravel provide any default method for that ?
Thanks
array_filter will remove empty elements:
$filtered = array_filter($request->all());

updateOrCreate() update null data if existed in database and only inserted new record

id_branch is PK, id_item is PK&FK
$id = B;
$id_selected = A;
$from_category= Category::where('id_branch', $id_selected)->get();
foreach ($from_items as $from_item) {
$test = Category::updateOrCreate(['id_branch' => $id,'id_item'=>$from_item->id_item], ['remarks' => $from_item->remarks]);
}
For example, user will select which branch category record need to be copied to the current branch category. After that will update or insert the record from the branch category selected to current one. Copy branch category A record to branch category B. If exist then update else insert. I able to insert the record but when update the value will be null. Anything wrong with my code?
updateOrCreate method take 2 parameters the first one is the conditions and the second one is the data to update or create.
In your case if the ID not exists in the branch, it will create a new row with the given data but you didn't pass the id_branch and id_item in the second parameter, I guess to fix this issue you should write the code like the following
$test = Category::updateOrCreate(
[
'id_branch' => $id,
'id_item'=>$from_item->id_item
],
[
'id_branch' => $id,
'id_item'=>$from_item->id_item,
'remarks' => $from_item->remarks
]
);

CodeIgniter: Using array within array

I am following nettut+ tutorial for pagination and to store POST inputs as querystrings in db. So far, everything works fine until, suppose if I get an array as POST input, i am unable to loop through it and get all the array values and to store into query_array (i.e., store array within array).
The snippets below:
$query_array = array(
'gender' => $this->input->post('gender'),
'minage' => $this->input->post('minage'),
'maxage' => $this->input->post('maxage'),
'Citizenship' => $this->input->post('citizenship'), // checkboxes with name citizenship[]
);
This returns only last stored array value in Citizenship.
The output array:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 )
makes the query string as:
&gender=1&minage=18&maxage=24&Citizenship=2
But, my requirement is to get all the values of 'Citizenship' array instead of last stored value.
The output required to make query string:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 [Citizenship] => 4 [Citizenship] => 6 )
The query string :
&gender=1&minage=18&maxage=24&Citizenship[]=2&Citizenship[]=4&Citizenship[]=6
Any help appreciated..
Thanks.
Doesn't look like code ignighter supports un-named multidimensional arrays as input without a bit of hacking.
If you can access raw $_POST data try replacing
$this->input->post('citizenship')
with
array_map('intval',$_POST['citizenship'])
Alternativly add keys to your post data:
&gender=1&minage=18&maxage=24&Citizenship[0]=2&Citizenship[1]=4&Citizenship[2]=6
I fixed it myself. I just looped through the POST array and got the individual array key & pair values.
foreach($_POST['Citizenship'] as $k => $v) {
$Citizenship[$v] = $v;
}
Hope this helps someone who face similar problem.

Resources