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 ..
Category : collections
I have data array like this : [ { fruit: "Apple", price: "10", }, { fruit: "Orange", price: "12", }, { fruit: "Manggo", price: "14", }, { fruit: "Grape", price: "16", }, { fruit: "Apple", price: "13", }, ] from my data how I make a search method to find my data with input type? ..
Dataset [ { "agent_id": "D01", "amount": "2" }, { "agent_id": "D01", "amount": "2" }, { "agent_id": "A01", "amount": "1" }, { "agent_id": "A01", "amount": "1" }, ] Expected Result [ { "agent_id": "D01", "amount": "4" }, { "agent_id": "A01", "amount": "2" }, ] Hello, there is an original dataset with 4 collections as shown above. ..
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 ..
I do have fetched all comments with relations done by load method (I checked it) and after that I had to search only for submitted comments so I used filter on collection. It returned me support/collection and after that I wanted to do something like $comment->thread (which thread was relation to another table) and the ..
I’m trying to make a ranking of elements in my webapp. I have this code: public static function getPetPlacing($pet_id) { $pets = Pet::get(); foreach ($pets as $key => $value) { $value->index = $key; if ($value->id == $pet_id) { return $value->index; } } return $pets; } Previous code makes a foreach loop, which after getting all ..
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’, ..
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 ..
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 ..
I want to compare two columns of a collection.I don’t want to do this in db level, only in collection level. I have a collection like this (I returned it in array model for readability) array:3 [ 0 => array:5 [ "total_amount" => 200000.0 "admin_max_amount" => "200000000" ] 1 => array:5 [ "total_amount" => 100000.0 ..
I recently wanted to sort parent elements of a collection by using their relationship values so I came across something like this and It works. Do you think this is a suitable method or am I making this overcomplicated? Basically, I want to sort the Events by their date Events have one to many with ..
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] => ..
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 ..
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’ => ..
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 ..
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 ..
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 ..
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 ..
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: ..
I am trying to use the Laravel Jetstream Teams for automated registration. Due to Jetstreams requiring a ‘personal team’ and my application requiring users to be apart of specific teams, I found a way around it to attach a user to a team and then switchTeam so their current team is loaded like their personal ..
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 ..
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 ..
I want to convert to this array $string = "1,[1,2,3],[2,2,4],2,3"; To Example 0 => 1, 1 => [ 0 => 1, 1 => 2, 2 => 3 ], 2 => [ 0 => 2, 1 => 2, 2 => 4 ], 3 => 2, 4 => 3 Sourc..
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 ..
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, ]; } } ..