Category : laravel-collection

I have an Eloquent collection with an eager loaded relationship. When I filter this eager loaded relationship, it works fine, but if I encode it as JSON (to pass it to my front end Javascript framework), the collection is no longer filtered. Simplified example: $questions = Question::with(‘variables’)->find($id); foreach($questions as $key => $q) { $questions[$key]->variables = ..

Read more

I have a many-to-many relationship as: class Game extends Model { public function players() { return $this->belongsToMany(‘AppPlayer’); } } class Player extends Model { public function games() { return $this->belongsToMany(‘AppPlayerGame’); } } I have 500k+ game_player entries, foreign keys are made. It costs a long time to get the result or even memory exhausted when ..

Read more

I am having issue to apply groupBy(‘dates) on activity_planners nested object of collection. AnyOne Can help? $this->with([ ‘customizeOutcomes’ => function($query) { $query->with([ ‘customizeOutputs’ => function($q) { $q->with([ ‘projectOutputs’ => function($qr) { $qr->with([‘output’, ‘projectOutputIndicators’ => function($qi){ $qi->with([‘outputIndicator’]); }, ‘projectActivities’ => function($qa) { $qa->with([‘activityPlanners’ => function($aq){ $aq->orderBy(‘dates’); }])->orderBy(‘position’); }]); }]); }]); } ])->find($id); The output: customize_outcomes: customize_outputs: ..

Read more

I am receiving data from database in this format { "status_code": "00", "data": { "id": 1, "applicant_id": 1, "book_date_time": "2020-12-31 13:00:00", "status": "On Lock", "reference_number": "R2361-3961000001", "questionnaire_ids": [ { "id": 1, "name": "Are you experiencing any of these symptoms? Please select all that apply", "answer": [ { "id": 1, "name": "Fever, chills, or sweating" } ..

Read more

I have the following piece of code : $callReport = array(); $callResult->each(function ($item) use (&$callReport) { $callCenterReport[$item->date][$item->callStatus] = $item->count; }); The $callReport result is : { "2020-11-25": { "x": 74, "y": 172, }, "2020-11-26": { "x": 10, "y": 49, } } But when i try with the collect methods : $callReport = collect(); $callResult->each(function ($item) ..

Read more

I have this result returned by Eloquent. Already sorted effective_at Ascending [ { "id": 2200155, "price": "0.07980", "effective_at": "2020-10-01" }, { "id": 2218010, "price": "0.07870", "effective_at": "2020-10-06" }, { "id": 2256374, "price": "0.07960", "effective_at": "2020-10-15" }, { "id": 2273713, "price": "0.08460", "effective_at": "2020-10-19" }, { "id": 2300540, "price": "0.08460", "effective_at": "2020-10-26" } ] I want ..

Read more

I have a Laravel collection of Carbon dates: $dates = collect([ Carbon::parse(‘2020-09-18’), Carbon::parse(‘2020-09-21’), Carbon::parse(‘2020-09-22’), Carbon::parse(‘2020-10-13’), Carbon::parse(‘2020-10-14’), Carbon::parse(‘2020-10-15’), Carbon::parse(‘2020-11-02’), Carbon::parse(‘2020-11-03’), Carbon::parse(‘2020-11-04’), Carbon::parse(‘2020-11-05′) ]); I want to split these into chunks where the dates are consecutive weekdays. I’ve managed to use chunkWhile to split the collection where the dates are either before or after: $chunks = $dates->chunkWhile(function ..

Read more

I wanna test my livewire store functionality. public function store() { $validated = $this->validate( [ ‘service_id’ => ‘required’, ‘price’ => ‘required|regex:/^[0-9]{1,5}([,.][0-9]{1,2})?$/’, ‘option_available.*’ => ‘boolean’, ‘option_price.*’ => ‘regex:/^[0-9]{1,5}([,.][0-9]{1,2})?$/’ ] ); // formatting price $validated[‘price’] = (float) str_replace(‘,’ , ‘.’, $validated[‘price’]); $service = $this->services->find($validated[‘service_id’]); $options = collect([]); if ( $this->serviceOptions ) { foreach ($this->serviceOptions as $key => ..

Read more

I have below array: $arr[123][‘price’] = 100; $arr[456][‘price’] = 500; $arr[100][‘price’] = 1000; $arr[511][‘price’] = 200; Now I want to sort it by price, so I did below operation: $arr = collect($arr); $sorted = $arr->sortBy(‘price’); Array gets sorted & dd($sorted) gives me below result: Collection {#957 ▼ #items: array:4 [▼ 123 => array:1 [▼ “price” ..

Read more

I need to display a list of category label from a filtered collection seperated by comma in blade views. The code looks like this {{$data->categories->filter(function ($value) {return $value->type_id == 1;})->pluck([‘label’])->toArray()}}; This error is when I use toArray() htmlspecialchars() expects parameter 1 to be string, array given I need the result in array to use it ..

Read more