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

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

Related

how to get data from a pivot table in a query at a controller

I have created a query in function at the controller where I want to get data from a pivot table based on the value sent using jquery.how can i create a function to get the values of the id associated with the selected house id in the controller.i have tried this but i get an error
$rentalcategoryhouses=Rental_house::whereIn('rentalcat_id',$rentalcategorydetails['catids'])
->with('housetags')->where('tag_id',$data['rentaltag'])->get();
the housetags here is the belongstoMany function in the house model that associates the houses with the tags.
function housetags(){
return $this->belongsToMany(Rental_tags::class,'rentalhouse_tags','rental_id','tag_id');
}
i want to get all the houses associated with the specific tag_id using a query in the controller.
this answer here solved my bug solution

Can't get ID from created Eloquent model extending Pivot in Laravel

Maybe simple, but I can't figure it out...
When I create a record using Eloquent and a model that extends Model, and then get its id right after it just works:
$example = Example::create(['name'=> 'exie']);
dd($example->id);
// returns id (ex. 15) as expected from the created record...
When I create a record using a model that extends Pivot and try to get id, it only returns null.
$customPivotExample = CustomPivot::create(['name' => 'custie']);
dd($customPivotExample->id);
// returns null instead of id...
The records all have a PK so I expected to just get the ID back, but apparently there is something about using a custom pivot model and getting it's id after creation what I am overlooking..
(examples are really simple but the actual code only contains more key=>value pairs and nothing more)
anyone has any idea?
Own Answer
Putting this here because this is not written (somewhat) in the Laravel documentation.
They mention this about auto incrementing ID's:
https://laravel.com/docs/9.x/eloquent-relationships#custom-pivot-models-and-incrementing-ids
I had not done this (my bad), but doing this also enables getting the ID after creation of a pivot record as in my second example....

I want to get the next id that will be created but not yet in laravel

I want to get the next id that will be created but not yet in laravel.
Is this code correct?
$tableStatus = DB::select("show table status from database_name where Name = 'table_name'");
if (empty($tableStatus)) {
throw new \Exception("Table not found");
}
// Get first table result, get its next auto incrementing value
echo $tableStatus[0]->Auto_increment;
Yes, the code is correct.
You can achive this in several other ways. For example, You can query the particular database table to extract last created id by ordering the rows based on created_at column or id column itself and finally increment the value by 1 to get next auto-increment value.

Laravel Eloquent - How do I select columns of the updated row?

I'm trying to update a single row,
using this query,
$update_row = PayRecord::where('date_from',$date_from)
->where->('date_to',$date_to)
->update(['status',1]);
what I want to do is to get the id of the updated row, without creating another query to get the id,
$get_updated_row_id = PayRecord::where('date_from',$date_from)
->where->('date_to',$date_to)'
->get('id');
Thank You,
Remove extra -> from where clause.
This may useful to you.
$update_row = PayRecord::where('date_from',$date_from)
->where('date_to',$date_to)
->first();
$update_row->update(['status',1]);
$update_row->id; // to get the id

return a row of a table using Active Record in Yii

I want to return a field of a row of table which has id = 4 in a model for example Post.
Which one should I use find() method or findByAttributes() ? And what is the correct syntax for it ?
To grab a model by its primary key I'd suggest `findByPk(). It's one of the most simple methods to use;
$id = 4;
$model = Post::model()->findByPk($id);
For other method syntaxes, have a read through the Yii Active Record wiki and Yii Active Record documentation, they're really helpful when starting out.

Resources