{"id":7888,"date":"2023-05-19T11:26:49","date_gmt":"2023-05-19T11:26:49","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=7888"},"modified":"2023-08-07T05:22:22","modified_gmt":"2023-08-07T05:22:22","slug":"laravel-when-with-greater-than-or-lesser-than","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/laravel\/laravel-when-with-greater-than-or-lesser-than","title":{"rendered":"Conditional clauses:  When() using Laravel"},"content":{"rendered":"<h3>Introduction:<\/h3>\n<p>Many cases we need to write conditional queries with laravel eloquent. For that we generally use \u201cif\u201d and \u201celse\u201d conditions and based on the condition we are used to make our query. Now laravel supports these types of conditional queries using the when() method.<\/p>\n<h3>Description:<\/h3>\n<p>Laravel provides a when() method to apply those kinds of conditional queries. Here we can understand by using the example.<\/p>\n<h3>Example:<\/h3>\n<p>For example we are used to make conditional queries like below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$role = $request-&gt;role;\r\n$query = Users::query();\r\n\r\nif($role==\u2019user\u2019)\r\n{\r\n    $query-&gt;where(\u2018role\u2019, \u2018user\u2019);\r\n}\r\nelse\r\n{\r\n    $query-&gt;where(\u2018role\u2019, \u2018admin\u2019);\r\n}\r\n\r\n$users =$query-&gt;get();\r\n<\/pre>\n<p>Now we can simplify this by using when() method provided by laravel<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n$users = Users::when($role, function (Builder $query, string $role) {\r\n      $query->where('role', $role);\r\n})->get();<\/pre>\n<p>In addition to this we can also pass value to the when method that needs to be checked.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n$query = User::query();\r\n\r\n$query->when(request('role', false), function ($q, $role) {\r\n    return $q->where('role', $role);\r\n});\r\n$users = $query->get();\r\n<\/pre>\n<p>We can also pass the callback\/clousar function as default value which needs to be executed only if the condition matched. We can pass greater than or less then conditions as well.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n$users = User::when(request('filter') == 'counts\u2019, function ($q) {\r\n    return $q->where('counts\u2019, '>', request('likes_count', 0));\r\n}, function ($q) {\r\n    return $q->orderBy('created_at', \u2018DESC\u2019);\r\n})->get();\r\n<\/pre>\n<p>More info: <a href=\"https:\/\/laravel.com\/docs\/master\/queries#conditional-clauses\" target=\"_blank\" rel=\"noopener\">https:\/\/laravel.com\/docs\/master\/queries#conditional-clauses<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Many cases we need to write conditional queries with laravel eloquent. For that we generally use \u201cif\u201d and \u201celse\u201d conditions and based on the condition we are used to make our query. Now laravel supports these types of conditional queries using the when() method. Description: Laravel provides a when() method to apply those kinds [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8479,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-7888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7888"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=7888"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7888\/revisions"}],"predecessor-version":[{"id":8480,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7888\/revisions\/8480"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8479"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=7888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=7888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=7888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}