{"action":"closed","issue":{"url":"https://api.github.com/repos/spatie/laravel-collection-macros/issues/13","repository_url":"https://api.github.com/repos/spatie/laravel-collection-macros","labels_url":"https://api.github.com/repos/spatie/laravel-collection-macros/issues/13/labels{/name}","comments_url":"https://api.github.com/repos/spatie/laravel-collection-macros/issues/13/comments","events_url":"https://api.github.com/repos/spatie/laravel-collection-macros/issues/13/events","html_url":"https://github.com/spatie/laravel-collection-macros/issues/13","id":173964572,"number":13,"title":"Split throws an error when collection is empty","user":{"login":"jstoone","id":1711456,"avatar_url":"https://avatars.githubusercontent.com/u/1711456?v=3","gravatar_id":"","url":"https://api.github.com/users/jstoone","html_url":"https://github.com/jstoone","followers_url":"https://api.github.com/users/jstoone/followers","following_url":"https://api.github.com/users/jstoone/following{/other_user}","gists_url":"https://api.github.com/users/jstoone/gists{/gist_id}","starred_url":"https://api.github.com/users/jstoone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jstoone/subscriptions","organizations_url":"https://api.github.com/users/jstoone/orgs","repos_url":"https://api.github.com/users/jstoone/repos","events_url":"https://api.github.com/users/jstoone/events{/privacy}","received_events_url":"https://api.github.com/users/jstoone/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-08-30T08:56:03Z","updated_at":"2016-09-01T08:38:02Z","closed_at":"2016-09-01T08:38:02Z","body":"When using chunk for splitting a collection into groups, the `$groupSize` has to be greater than 0, or else chunk will throw an error.\r\n\r\nSo when using the following calculation `ceil($this->count() / $numberOfGroups)`, if the collection is empty, `$this->count()` returns 0 and`chunk()` will throw an error.\r\n\r\nIn my own projects I've been doing the following to handle empty collections in blade instead:\r\n```blade\r\n<?php\r\n    $groupSize = 4;\r\n?>\r\n<div class=\"row\">\r\n@foreach(range(1,$groupSize) as $index)\r\n    <div class=\"col-md-3\">\r\n\r\n    @foreach($user->posts->forPage($index, $groupSize) as $user)\r\n        <h1>{{$user->name}}</h1>\r\n    @endforeach\r\n\r\n    </div>\r\n@endforeach\r\n</div>\r\n```\r\n\r\nSo before I make a pull-request for handling the zero value, I'd like to know whether this is the intended behavior, to mimic the `chunk()`, or if it would make sense to just return the collection itself?\r\nIf following `chunk()` is the plan, I think an exception should get called with another message, since the user would be scratching their heads, looking for a call to `array_chunk`.\r\n\r\nSo as a recap, I have two solutions in mind:\r\n1. Make an early return of `$this`, if the `count() == 0`\r\n2. Throw a custom exception: _\"split(): Size parameter expected to be greater than 0\"_\r\n"}}
{"actor":{"display_login":"sebastiandedeyne"}}