Category : laravel-collection

$courseId = CourseFiles::where(‘subjectId’, $subjectId)->get([‘courseId’]); $output = []; foreach ($courseId as $key => $row) { $id = Course::find($row); // dd($id); $fileId = $id[0][‘fileId’]; } dd($id); gives the below result. As $id is a collection, i can only fetch data from this as $id[0][‘fileId’]. dd($id->fileId); & dd($id[‘fileId’]); are not working. Although this works fine in my local ..

Read more

To make it easier to understand the problem, I will hardcode the data that I am using the collection on and explain the problem. Let us assume the following data structure in JSON format, { "shelters_with_linear_distances": [ { "id": 3, "shelterName": "Third Shelter", "latitude": "5.0034000", "longitude": "70.1230000", "linear_distance": 3.1352984845527 }, { "id": 4, "shelterName": "Fourth ..

Read more

I have a serialized String like this $string = ‘[{"name":"FOO"},{"name":""},{"name":"BAR"}]’; I am trying to process it via Laravel Collection’s filter method and eliminate items without a defined "name" property. $collection = collect(json_decode($string)); $collection = $collection->filter(function($v){ return !empty($v->name); }); $string = json_encode($collection->toArray()); dd($string); Normally I am expecting something like this: [{"name":"FOO"},{"name":"BAR"}] But I’m getting something like ..

Read more

I need help to fix a problem related to dealing with laravel collection methods. I am creating a learning website that have courrse and course episode I have used meilisearch to search in course_episode table. $query = $request->s; $episodes = CourseEpisode::search($query) ->get(); after getting episodes, I have used map and reject methods of collections. $episodes->map(function ..

Read more

I want to convert all of my static data to collection in Laravel. This is my data: static $menu_list = [ [ ‘path’ => ‘admin/report/transaction’, ‘active’ => ‘admin/report’, ‘name’ => ‘Report’, ‘icon’ => ‘file-text’, ‘children’ => [ ‘path’ => ‘admin/report/transaction’, ‘active’ => ‘admin/report/transaction’, ‘name’ => ‘Transaction’, ], ], ]; This function converts my data to ..

Read more

I am using spatie translation package in laravel. I have collection which i want to sort by name but the name column has a json collection due to which its not getting sorted. I have tried $shoptype->categories->sortBy(‘name.en’); and my result { "id": 8, "type_id": 1, "name": "{"en":"Other Meats","es":"Otras Carnes"}", "image_path": "http://market.test/uploads/category/Cpaf7nCVZxshoptype.png", "created_at": "2021-04-02T12:13:14.000000Z", "updated_at": "2021-08-14T06:17:35.000000Z", ..

Read more

I want to pass an additional parameter from controller to collection resource, Please have a look at the following code, I don’t know why It’s not working. I might be doing something wron here, please help Controller $batchStudents = $batch->students->except($batch->host->id); $batchStudents->test = ‘This is testing’; $students = UserCollection::collection($batchStudents); Collection: class UserCollection extends JsonResource { return ..

Read more

I am not sure, what is going on here. I have a collection of Model ID’s but want to fallback on using all if specific ID’s are omitted. So I have this code: use AppModelsPost; function list($ids = []) { $posts = collect($ids)->whenEmpty(function ($posts) { return Post::all()->pluck(‘id’); })->each(function ($item, $key) { $post = Post::findOrFail($item); }); ..

Read more

Consider books and authors relationship. Every book belongsTo an author and 1 author hasMany books. Books table has fields like (Id,Title,Version etc) and authors have fields like (Id,Name,Address etc). Note DB columns do not follow the default laravel naming convention. Now; I want to fetch some fields from both tables. More speficic, I want title,version,name ..

Read more

"Exception Property [delivery_cycle] does not exist on this collection instance." Error occured. But the property exists as far as I can see. How should I fix it? here is log. local.DEBUG: :TEST_ABC: [{"IlluminateSupportCollection":[{"id":80,"company_id":96,"plan_id":1,"start_ym":202107,"end_ym":null,"created_at":"2021-06-18 15:47:01","updated_at":"2021-06-18 15:47:01","deleted_at":null,"delivery_cycle":3,"delivery_time":2,}]}] delivery_cycle:3 is displayed. my code $contracts = DB::table(‘company_contracts’)->where(‘company_id’, $request->id)->get(); Log::debug(":"."TEST_ABC:", [$contracts]); if($contracts->delivery_cycle === config(‘const.DELIVERY_CYCLE.ONECE_A_MONTH’)){} Sourc..

Read more

I need help on how to read this line, Im new to php Laravel and still grasping the concept. Cheers lad ->where(function ($someVariable) use ($anotherVaraible) this is my my query $sibling_list = []; $sibling_list_items = YommFtRelationship::whereIn(‘type_id’, $sibling_ids) ->where(function ($query) use ($sibling_list_ids) { $query->whereIn(‘member_1_id’, $sibling_list_ids) ->orWhereIn(‘member_2_id’, $sibling_list_ids); }) ->distinct() ->get([‘member_1_id’, ‘member_2_id’]); Sourc..

Read more

I am using Laravel ViewModels to refactor the data returned from an API. The following is a function Im using to format the particular data public function similar() { $similarCollection = collect($this->similar)->map(function($movie){ return collect($movie)->merge([ ‘poster_path’ => $movie[‘poster_path’] ? ‘https://image.tmdb.org/t/p/w500/’.$movie[‘poster_path’] : ‘https://via.placeholder.com/500×750?text=’.$movie[‘title’], ‘backdrop_path’ => $movie[‘backdrop_path’] ?’https://image.tmdb.org/t/p/original/’.$movie[‘backdrop_path’] :’https://via.placeholder.com/500×750?text=’.$movie[‘title’], ])->only([ ‘poster_path’,’title’,’backdrop_path’,’backdrop_path’ ]); }); dd($similarCollection); } The above function ..

Read more

I have the following nested array from this code snippet return collect($this->similar)->dd(); array:20 [▼ 0 => array:14 [▼ "poster_path" => "/wV9e2y4myJ4KMFsyFfWYcUOawyK.jpg" "title" => "Bambi" "video" => false "id" => 3170 "overview" => "Bambi’s tale unfolds from season to season as the young prince of the forest learns about life, love, and friends." "release_date" => "1942-08-14" ..

Read more

This code from product_listing.blade.php and here the dropdown filter. I need all the item name in ascending orde. Right now the list is in order how I can see it in a database table.. <div class="box-title text-center"> {{ translate(‘Filter by CarType’)}} </div> <div class="box-content"> <div class="filter-checkbox"> <select class="form-control" data-placeholder="{{ translate(‘All CarType’)}}" name="cartype" onchange="filter()"> <option value="">Select ..

Read more

I have a collection grouped (multiple times) like this: IlluminateSupportCollection {#1883 ▼ #items: array:1 [▼ 57082 => IlluminateSupportCollection {#1885 ▼ #items: array:1 [▼ "07-2021" => IlluminateDatabaseEloquentCollection {#1863 ▼ #items: array:1 [▼ 343 => IlluminateDatabaseEloquentCollection {#1864 ▼ #items: array:1 [▶] } ] } ] } ] } I have another collection that could have same values ..

Read more