php - is that possible to get data once and the perfrorm query on that instead of database in laravel?
Get the solution ↓↓↓Is that possible to get data at once and then perform query on it instead of database. Because I am doing calculation for almost 500 customers. for while i need to run almost 100 queries for each customer in database with same pattern , that why i am asking that if I can get data at once and then perform query on that data instead of database. because each time for each customer increase mysql cpu consumption
What i am doing right now
foreach($customers as $customer)
{
$customer_charge= CustomerCharge::where('customer_id',$customer->id)->with('rate_charge')->with('chilled_rate_card')-->with('frozen_rate_card')->get()
$this->calculateConsignment($customer_charge);
}
I want to do like that
$customer_charge= CustomerCharge::whereIn('customer_id',[array])->with('rate_charge')->with('chilled_rate_card')-->with('frozen_rate_card')->get()
Now find() here customer charge form $customer_charge object instead of database
Is that possible if yes then how?
Is that possible ?
Answer
Solution:
Yes.
When you perform a query, Laravel returns to you an Eloquent Collection which allows on which you can still call the same methods to retrieve a specific data:
$customer_ids = $customers->pluck('id'); // Assuming $customers is an Eloquent Collection
$charges = CustomerCharge::whereIn('customer_id', $customer_ids)
->with('rate_charge')
->with('chilled_rate_card')
->with('frozen_rate_card')
->get();
foreach($customers as $customer)
{
$customer_charge = $charges->where('customer_id', $customer->id)->first(); // querying the already retrieved $charges data, not the database
$this->calculateConsignment($customer_charge);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.