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?
Related
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
I run in a strange issue, I need to sum a column using eloquent but every function I use eloquent do not consider the decimal.
First of all I have the column set ad DECIMAL(8,2) and in my DB the data is stored in the correct way.
If I just use a simple query:
select sum(total_after_tax) from invoice_details where invoice_id = 1
I get the result: 312.93
Now if I use eloquent:
$invoice = Invoice::with(['details'])->get();
dd($invoice->first()->details->sum('total_after_tax'));
I get: 312.
How is this possibile? How can I get the correct value form DB?
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
There are some duplicate rows in db table. When retrieving from db and setting for the drop box, it should display values uniquely. How should I perform that?
There I want to avoid duplicating 2 in the drop box
I you have an instance of Illuminate\Support\Collection than you could do something like this
$unique = $collection->unique(); // where $collection is of course the variable that holds your collection
$unique->values()->all(); // [1,2,3,4]
I'm trying to update additional column data in a pivot table in a many to many relationship.
I have two tables - reservation and resource linked with a pivot table. I can attach and am working with the model. However I'm struggling to update one of the additional columns in the pivot table.
I have an object: '$reservation' From that object I created another object $resources using:
$resources = $reservation->resource()->get();
I'm then iterating through $resources using a foreach loop as follows
foreach($resources as $resource ) {...}
I then want to update a column called gcal_id and am using the following:
$resource->pivot->gcal_id = "TEST";
$resource->save();
If I var_dump the model I can see the property exists to the correct value but in the database itself the entry is not being updated - so the save is not working
I have the columns listed in both sides of the relationship with this:
->withPivot('start', 'end', 'quantity', 'product_id','gcal_id')
Given I have the resource object how can I update a column correctly in the pivot table and save to database?
Thanks
After that you set the attribute on the pivot:
$resource->pivot->gcal_id = "TEST";
You seem to save the resource and not the pivot:
$resource->save();
If you want the pivot to be saved, saving the resource is not enough. Call save on the pivot instead:
$resource->pivot->gcal_id = "TEST";
$resource->pivot->save();
An alternative instead of save method, is the method Illuminate\Database\Eloquent\Relations\BelongsToMany\updateExistingPivot()
$reservation->resource()->updateExistingPivot($resource_id, ['gcal_id' => 'TEST']);
Or
$reservation->resource()->updateExistingPivot([1, 4, 5, 6], ['gcal_id' => 'TEST']);
This work for me in Laravel 4.2