Bulk Insert or Update Laravel Eloquent without Foreach Loop - laravel

Data structure is as follows:
referral_statistics
id | registered_user_id | date | promoter_ref_revenue | writer_ref_revenue
Now for this Table I want to insert two arrays,
first one will is successfully getting inserted as follows:
$referralStats = ReferralStatistic::insert($userReferralStatsArray);
$referralStats = ReferralStatistic::insert($userWriterReferralStatsArray);
Now second array is $userWriterReferralStatsArray, while inserting this array I want to check if same date and same registered_user_id is repeated then it should update not insert the new row. How can I do it, or please suggest me if there is any other good way to implement it because there are large number of rows.
Thanks in advance

Instead of using the insert() method you can use the updateOrCreate() Eloquent method or simply call the save() method of the model which updates the model if it exists

Related

How to get the list of inserted ids from the insert() method to use with the syncWithoutDetaching() method?

I have Files and Folders models that are related by a many-to-many relationship with a folder_file pivot table.
I want to insert files in bulk using the insert() method, then use the list of inserted ids with the syncWithoutDetaching() method to update the pivot table, | first create the $data_array in a foreach loop that does other things as well:
$data_array = [];
foreach (...) {
// ...
$data_array[] = $row;
}
then I want to insert the data and update the pivot table:
File::insert($data_array);
$folder->files()->syncWithoutDetaching($array_of_inserted_ids);
Is it possible to get the list of inserted ids from the insert() method?
Or maybe there is a more efficient way for better DB performance?
At first I just wanted to create the record and update the pivot table inside the foreach loop that creates the $data_array so that on every iteration it would create and update the tables, but that could be bad for performance with that many queries?
You could use the createMany() here. Instead of inserting the files then get the ids of inserted data. It will accept an array of data so that you could directly insert your array of files then connecting them to the folder.
$folder->files()->createMany($data_array);

Laravel eloquent form: multiselect not persisting in edit

I have a eloquent model say Math which has a column in db say number, which is of type array
in db suppose,
for id =1 number = [1,2]
When I try to edit this entry from form, I have to select values 1,2 again. As in the data is not persisting
if($form->isEditing()){
$number = Math::where('id', 1)->pluck('number', 'number');
$form->multipleSelect('number')->options(NumberList::all()->pluck('id', 'id'))->default($number);
}
I have tried doing the above, but the data is not persisting in edit mode. How to solve this?

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

Deleting multiple same id from mysql database

Below is my code for deleting all rows with ids one.It seems that only first index was deleted
and an error will trigger right away.How can i loop through the array and delete all rows with id one.
The array I've created here is only a representation from my database with multiple columns.
Any idea in resolving this problem is much appreciated.
public function deleteRow(){
$ids = ['1','1','1','3','3','1','1'];
foreach($ids as $id ){
$id = Scholarshipcount::find($id);
$id->delete();
}
}
my error
Call to a member function delete() on a non-object
Find() can fail to find the record, especially of you have multiple times the same PK as in your example (and you have already deleted the object the first time). You might want to consider using findOrFail instead.
You should use a different kind of approach instead. Assuming that your table name is scholarshipcounts then:
DB::table('scholarhipcounts')->whereIn('id', $ids)->delete();

Select one column with where clause Eloquent

Im using Eloquent. But I'm having trouble understanding Eloquent syntax. I have been searching, and trying this cheat sheet: http://cheats.jesse-obrien.ca, but no luck.
How do i perform this SQL query?
SELECT user_id FROM notes WHERE note_id = 1
Thanks!
If you want a single record then use
Note::where('note_id','1')->first(['user_id']);
and for more than one record use
Note::where('note_id','1')->get(['user_id']);
If 'note_id' is the primary key on your model, you can simply use:
Note::find(1)->user_id
Otherwise, you can use any number of syntaxes:
Note::where('note_id', 1)->first()->user_id;
Note::select('user_id')->where('note_id', 1)->first();
Note::whereNoteId(1)->first();
// or get() will give you multiple results if there are multiple
Also note, in any of these examples, you can also just assign the entire object to a variable and just grab the user_id attribute when needed later.
$note = Note::find(1);
// $user_id = $note->user_id;

Resources