Category : collections

New to Laravel. Have what seems like should be a non-issue, but is causing a headache. I’m trying to insert a key-value pair (bookingRef) within the result object/collection returned, such that the result would be: [{"class_id":7,"class_name":"beginner","class_slots_avail":100,"class_slots_booked":53,"class_date":"2020-12-07 21:47:23","class_time":"09:25:00","class_reg_price":350, bookingRef: 127}] I’ve tried methods such as push, put and merge that will insert the key-value after the ..

Read more

This is my code: public function search_clients($names){ $user = Auth::user(); $all_clients = $user->clients; // Retrieve object of all clients of user $names = ["Pera","Peric"]; // Pera – name, Peric – surname $collect = collect($all_clients); $klijenti = $collect->filter(function($query) use ($names){ $query->whereIn(‘name’, $names); $query->orWhere(function($query) use ($names) { $query->whereIn(‘surname’, $names); }); return $query; }); return $klijenti; } I ..

Read more

I have a collection of Measures $m = new Measure([ ‘time’ => $measure[‘timestamp’], ‘operation_id’ => $measure[‘op_id’], ‘meter_id’ => $prm, ‘conso_prod’ => $measure[‘conso_prod’], ‘delta’ => $measure[‘delta’], ‘redistributed’ => $measure[‘redistribute’], ] Measure table is a postgres hypertable(timescaleDB extension) time is a timestamp without timezone When I want to sort it, I can do it with: $myCollection->sortBy([‘operation_id’, ‘meter_id’, ..

Read more

I am fetching data from database. then I am adding new value in that object. after that i am trying to sort object that value but it is not working Here is the code $providers= Provider::query(); $providers = $providers->get(); foreach($providers as $provider) { $provider->distance = $this->addDistanceToProvider($provider,$request); } $providers->sortBy(‘distance’); dd($providers); How I can sort is by ..

Read more

I display the results of two collections with foreach as shown below but I tried several ways to create a new collection that can’t be used for the third foreach $users = User::with(‘absen’)->get(); $period = CarbonPeriod::create(‘2020-11-25′,’2020-11-30’); foreach ($period as $date) { foreach ($users as $user) { echo $date->format(‘Y-m-d’); echo "–>"; echo $user->nama; echo "–>"; foreach ..

Read more

when i use $data_collect->groupBy(‘group_id’) and give me result like this: IlluminateSupportCollection Object ( [items:protected] => Array ( [1] => IlluminateSupportCollection Object ( [items:protected] => Array ( [0] => stdClass Object ( [id] => 1 [title] => Item A [group_id] => 1 [price] => 4000 ) [1] => stdClass Object ( [id] => 22 [title] => ..

Read more

public function show($id) { $post = Post::find($id); $comments = $post->comments()->orderBy(‘created_at’, ‘desc’)->get()->transform(function ($item, $key) { $item->registered = $item->created_at->diffForHumans(); return $item; }); return view(‘blog_views.post’)->with([‘post’ => $post, ‘comments’ => $comments]); } I want to add paginate(5) on $comments before return it to view and keep this values accessible in the view. {{ $comment->user->name }} relationship returning the creator ..

Read more

I’m trying to find a way to get the minimum amount of a collection and return it with the id as well. Pretty sure there is a quick way around it but could not find a solution yet. $collection = collect(); $collection->push([‘id’ => 3, ‘count’ => 24]); $collection->push([‘id’ => 43, ‘count’ => 14]); $collection->push([‘id’ => ..

Read more

Apology if my title confuses you. Here’s my problem. in my controller public function create() { $users = User::whereHas(‘roles’, function ($q) { $q->where(‘roles.name’, ‘=’, ‘collector’)->orWhere(‘roles.name’, ‘=’, ‘borrower’); }) ->with(‘profile’) ->get() // What I’m trying to achieve is something like this. ->pluck(‘profile.first_name’, ‘&&’, ‘profile.first_name’,’id’); return view(‘dashboard.documents.create’, compact(‘users’)); } What I’m trying to achieve is something like ..

Read more

I’ve got a simple LessonUser table and model – 3 columns + timestamps. I’m using Laravel 8 and doing TDD. I grab some data from the table: /** get the latest lessonUser for each lesson_id */ $latestUserLessons = LessonUser:: select(‘user_id’, ‘lesson_id’, ‘status_id’) ->where(‘user_id’, $user) ->latest() ->get() ->groupBy(‘lesson_id’) ; If I run this: foreach ($latestUserLessons as ..

Read more

This is the array that I am trying to convert to collections and pluck the values. $arr = [ "TID" => "81226042", "TLineID" => "81226042", "Sales" => [ [ "TLineID" => "226041" ], [ "TLineID" => "226042" ], [ "TLineID" => "9042" ] ] ]; //$r = collect($arr)->pluck(‘Sales’); $r = collect(json_decode(json_encode($arr)))->pluck(‘Sales’); print_r($r->toArray()); the output I ..

Read more

I have the following code that generates a simple collection: public function groupByClass($centers) { // group by $centers = $centers->map(function($center,$index){ $vg = $center->vehicles->groupBy(‘class.name’); // add sum prop $newVg = $vg->map(function($vehicles,$classIndex){ $obj = ‘{"makbas_sum":1,"mahata_sum":1,"temp_sum":1,"dsem_sum":1,"nahrawan_sum":1}’; $obj = json_decode($obj); $vehicles->put(‘sum’,$obj); return $vehicles; }); $center[‘vg’] = $newVg; unset($center->vehicles); return $center; }); //dd( $centers[0]->vg[‘كيا’][‘sum’]); // it is working and new ..

Read more

I’m facing an issue in dealing with the returned generated collection from another class, I have 2 classes, one called "FCount" that is responsible for generate a collection from the database and append a new property called "sum" for each object inside the generated collection using a function inside this class called "getDataOfIntervalV2". class code: ..

Read more

I have a chunked array of 1000 Raw Objects: class Raw extends Model { protected $table = ‘raws’; protected $casts = [ ‘time’ => ‘datetime’ ]; public $timestamps = [ "time" ]; private string $operationId; private string $meterId; private string $consoProd; private string $timestep; private string $unit; private string $timestamp; private string $value; } collect($measures)->chunk(1000)->each(function ..

Read more

I receive a big collection in a POST, which size is 1062720 $raws = collect($raws)->map(function ($raw) { $raw[‘time’] = $raw[‘t’]; $raw[‘delta’] = $raw[‘d’]; return AppModelsRaw::factory()->make($raw); })->toArray(); I have manipulate the array: rename t, d to time,delta. In each Raw, I also need to inject several static fields: operation_id, meter_id, conso_prod, timestep, unit To insert it ..

Read more

I have a collection: { "2020-09-06 12:00:00": [ { "usage_point_id": "12345678912345", "key": "0.7680189" }, { "usage_point_id": "12345678912346", "key": "0.29539188" }, ] } I would like to transform it to: { "timestamp": "2019-12-18T10:30:00Z", "keys": [ { "usage_point_id": "12345678912345", "key": "12,5" }, { "usage_point_id": "12345678912346", "key": "0.29539188" }, ] } They question is how to change: "2020-09-06 ..

Read more

My code for querying temporary table $limit = 5; $data = DB::table(‘temp_mappings’)->paginate($limit); return SubCapacityResource::collection($data); this my SubCapacityResource code class SubCapacityResource extends JsonResource { /** * Transform the resource into an array. * * @param Request $request * @return array */ public function toArray($request) { return [ ‘name’ => $this->name, ‘capacity’ => $this->capacity, ]; } } ..

Read more