Get and update data in one query Laravel - laravel

$wor= Warehouse_other_receive::where('or_no', $id)->first();
Warehouse_other_receive_detail::where('or_no', $id)->update(array('or_date'=>$wor->or_date);
$wor->update(['status' => '1']);
My goal is to get data and update it in one query in my Laravel project. On the first-line I get data from wharehouse_other_receive model, on the second line update data form first line get data the third line is after second-line code update Warehouse_other_receive model

Related

Update json column value in eloquent model in laravel - Database-Oracle

In my project current_profile is a JSON object column for EmployeePromotionAndShifting model. I want to update a key named division_i of current_profile column.
The JSON object is like this:
"current_profile" =>
{"staff_id":"2927","division_id":"84","department_id":"200","unit_id":null}
I am getting an error:
"This database engine does not support JSON operations." at
vendor\yajra\laravel-oci8\src\Oci8\Query\Grammars\OracleGrammar.php:1
Code to update in database!
EmployeePromotionAndShifting::where('staff_id',$currentProfile['staff_id'])->update(['current_profile->division_id' => 555,]);
I want to update division_id key of current_profile column for a particular row.

Laravel - How to get last record from a table and update another table

I am using Laravel 5.8. I have this function in my api controller
$bill=new Bill();
$bill->msisdn =$msisdn;
$bill->trans_id =$transReference;
$bill->ext_id =$ref_id;
$bill->amount=$game_check->amount;
$bill->answer=$useroption;
$bill->game_code=$useresponse;
$bill->is_complete_transactions =1;
$bill->billing_channel="Unity-7799";
$bill->save();
I have another table user_response, the model class is User_Response. It has these fields:
id, msisdn, answer, answer_code
I want to get the last record from $bill=new Bill() where $bill->msisdn is equal to msisdn in User_Response. Then update answer in User_Response with $bill->answer.
Please how do I achieve this?
So save() method always return the recently save data in database.
if you do $bill->save()
so the last inserted record can be achive by the same object you trigger save() from.
For eg i need last inserted id from the database after insertion.
so
$bill = new Bill();
$bill->myFileds = $request->myfields;
$bill->save();
$lastInsertedId = $bill->id;
as you can see from above code. the last inserted record is $bill. you can use it for User_Repsonse
User_Response::where('msisdn',$bill->msisdn)->update(['answer'=>$bill->answer]);
Here you go

Laravel 5.5 Call to undefined method stdClass::update()

Laravel 5.5 show me the error message:
Call to undefined method stdClass::update()
for the following codes in the Model
DB::table('adminUserLogs')
->where('adminUserId', $id)
->where('adminUserOutTime', NULL)->first()
->update(['adminUserOutTime' => \Carbon\Carbon::now()]);
Try using ->limit(1) instead of ->first() in the chain.
In Laravel if you wish to update only one record then you can use ->first() method of Eloquent ORM to get eloquent object and then you can simply update the object by ->save().
Laravel query builder directly not supporting update only record as Update with limit of 1 is not directly supported by mysql MySQL - UPDATE query with LIMIT
In the example you have given here is some modification to be work,
DB::table('adminUserLogs')
->where('adminUserId', $id)
->where('adminUserOutTime', NULL)
->update(['adminUserOutTime' => \Carbon\Carbon::now()]);
This will result in updating the records where specified conditions. To update specific record pass id column in where clause of that particular record.

How to handle new incoming entries while paging through posts?

Let's assume I have a pagination like this
return App\Post::paginate(1);
After loading this someone creates a new entry in the database for posts. How can i ensure that the "second page" of my pagination does not return the first one again?
Will I always need to send a timestamp with every request to make an additional query before pagination is used?
like
App\Post::where('created_at', '<', $request->input('max_created_at'))->paginate(1);
You can use orderBy. You could do something like that:
// Get the last post (most recent)
return App\Post::orderBy('created_at', 'DESC')->paginate(1);
Or:
// Same as above
return App\Post::orderBy('created_at', 'DESC')->first();
orderBy means all you result are sorted in your query. Hope it will help.
You don't need to pass any timestamp value to verify pagination.
In the controller, in view file you need to pass result of this query.
$result = App\Post::orderBy('created_at', 'desc')->paginate(1);
return view('pagination',compact('result'));
In view file you need below line for pagination work automatically as per Laravel syntax.
{{ $result->links() }}
Visit https://laravel.com/docs/5.2/pagination/ how laravel pagination work.

Yii2- Not able to insert/delete rows using console controller

I am trying to implement email queue in my project which uses CRON.
I am adding a row in one table and deleting a row from another table using CreateCommand of yii2. Database is mysql.
The entire code is executed and it does not display any error. But the rows are not added or deleted from the database.
I have also tried doing the same thing using ActiveRecords but it does not work. Hence I have used CreateCommand.
Below is the code which I am using -
$connection = \Yii::$app->db;
$result = $connection->createCommand()->insert('email_queue_completed', $arrInsert)->execute();
if(!empty($result))
{
$connection->createCommand()
->delete('email_queue', ['id' => $mail->id])
->execute();
}
$arrInsert is an array containing key value pairs of values to be inserted.

Resources