{"id":567,"date":"2020-06-06T14:25:12","date_gmt":"2020-06-06T14:25:12","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=567"},"modified":"2020-06-06T14:28:44","modified_gmt":"2020-06-06T14:28:44","slug":"can-you-test-emptiness-of-laravel-collection-using-empty","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/can-you-test-emptiness-of-laravel-collection-using-empty.html","title":{"rendered":"Can You Test Emptiness of Laravel Collection using empty()?"},"content":{"rendered":"\n<p>In short, Yes and No. Not the way we commonly do a variable, but in laravel way yes. It&#8217;s a common mistake done by almost all the laravel developer once in a lifetime until the bug appears (Well, you are not counted, if you are exceptional :P). So, let&#8217;s explore. <\/p>\n\n\n\n<p>Let&#8217;s look at how laravel collection is constructed. Go to your laravel tinker console and try this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php artisan tinker\nPsy Shell v0.9.12 (PHP 7.2.31 \u2014 cli) by Justin Hileman\n>>> $collection = collect([])\n=> Illuminate\\Support\\Collection {#3164\n     all: [],\n   }\n<\/pre>\n\n\n\n<p>You see, when I create an empty collection, laravel still puts an underlying array called &#8216;all&#8217;. This is the manipulator array and contains all the content inside. This array is accessible through the collection all method:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> $collection->all()\n=> []<\/pre>\n\n\n\n<p>You see, the return is an empty array. But when it&#8217;s just the collection, it&#8217;s not really empty, it has an underlying content holder. <\/p>\n\n\n\n<p>So, how can we test emptiness of the collection? Well, there are 3 ways.<\/p>\n\n\n\n<p>I) Laravel gives a way to return the number of element in the collection with a collection method calls count(). You can test it against 0 to see if the collection is empty or not<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> $collection->count()\n=> 0<\/pre>\n\n\n\n<p>II) You may use the regular php count() method to return that it doesn&#8217;t contain any leaf element and test it against 0:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> count($collection)\n=> 0<\/pre>\n\n\n\n<p>III) If you are a big fan of &#8217;empty&#8217; and still would like to follow, then you can grab the content of the collection using all method and test it against empty as following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> empty($collection->all())\n=> true<\/pre>\n\n\n\n<p>So, yeah, now you know all the ways \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In short, Yes and No. Not the way we commonly do a variable, but in laravel way yes. It&#8217;s a common mistake done by almost all the laravel developer once in a lifetime until the bug appears (Well, you are not counted, if you are exceptional :P). So, let&#8217;s explore. Let&#8217;s look at how laravel &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/can-you-test-emptiness-of-laravel-collection-using-empty.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Can You Test Emptiness of Laravel Collection using empty()?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[272,273,271,25],"tags":[396,319,397,398],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/567"}],"collection":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":3,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":570,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/567\/revisions\/570"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}