{"id":11381,"date":"2020-02-29T06:40:12","date_gmt":"2020-02-29T06:40:12","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=11381"},"modified":"2021-11-12T06:45:08","modified_gmt":"2021-11-12T06:45:08","slug":"conditional-if-else-elseif-isset-empty-and-unless-statements-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/conditional-if-else-elseif-isset-empty-and-unless-statements-in-laravel\/","title":{"rendered":"Conditional (@if, @else, @elseif, @isset, @empty, and @unless) Statements in Laravel."},"content":{"rendered":"\n<p>First of all, we make a <strong>condition.blade.php<\/strong> file in which we define our conditions. See the below image.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"577\" height=\"186\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Condition-view.png\" alt=\"\" class=\"wp-image-11388\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Condition-view.png 577w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Condition-view-300x97.png 300w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><\/figure><\/div>\n\n\n\n<p>After that, we make a function named <strong><em>condition<\/em><\/strong> in the controller and Defined a <em>variable<\/em> <strong>$name<\/strong> and assigned a <em>string<\/em> <strong>Sushant<\/strong> into it. After that returned it to view(<em>condition.blade.php<\/em> file is in a test folder). See the below image for help.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"429\" height=\"124\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/condition-controller.png\" alt=\"\" class=\"wp-image-11389\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/condition-controller.png 429w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/condition-controller-300x87.png 300w\" sizes=\"auto, (max-width: 429px) 100vw, 429px\" \/><\/figure><\/div>\n\n\n\n<p>After that, we configure our Route. See below how I configured, <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"97\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Condition-route.png\" alt=\"\" class=\"wp-image-11393\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Condition-route.png 406w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Condition-route-300x72.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/figure><\/div>\n\n\n\n<h1 class=\"has-text-align-center has-vivid-red-color has-text-color wp-block-heading\">@if and @else Condition <\/h1>\n\n\n\n<p>Condition wrote under <strong><em>if<\/em><\/strong> statement is executed when it&#8217;s <strong><em>True<\/em><\/strong> otherwise <strong><em>else<\/em><\/strong> executed. See the below Example:- <\/p>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">Sample Program<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"216\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/if-else-condition-ststement-laravel.png\" alt=\"\" class=\"wp-image-11405\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/if-else-condition-ststement-laravel.png 541w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/if-else-condition-ststement-laravel-300x120.png 300w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><\/figure><\/div>\n\n\n\n<p>In  the above program, <strong>if-statement<\/strong> is true so it gives output <strong>String is &gt; 0.<\/strong> See the output below:-<\/p>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">Output<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"331\" height=\"133\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/if-else-laravel-output.png\" alt=\"\" class=\"wp-image-11409\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/if-else-laravel-output.png 331w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/if-else-laravel-output-300x121.png 300w\" sizes=\"auto, (max-width: 331px) 100vw, 331px\" \/><\/figure><\/div>\n\n\n\n<h1 class=\"has-text-align-center has-vivid-red-color has-text-color wp-block-heading\">@elseif Condition <\/h1>\n\n\n\n<p>The <strong>else-if <\/strong>statement is written after <strong>if<\/strong> and before <strong>else<\/strong> statement and it executes when <strong>if<\/strong> statement is false. See the below example:- <\/p>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">Sample Program<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"273\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/elseif-program-laravel.png\" alt=\"\" class=\"wp-image-11419\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/elseif-program-laravel.png 499w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/elseif-program-laravel-300x164.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/figure><\/div>\n\n\n\n<p>In the above program, <strong>if<\/strong> condition is false so <strong>elseif <\/strong>condition checked whether it&#8217;s true or false, if it&#8217;s true then it executed otherwise <strong>else<\/strong> condition executed. But in the above program <strong>elseif<\/strong> condition is true, so it&#8217;s executed and gives the output:- <strong><em>String is in between 5 and 10.<\/em><\/strong> See the output below.<\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">Output <\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"323\" height=\"139\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/elseif-output-laravel.png\" alt=\"\" class=\"wp-image-11427\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/elseif-output-laravel.png 323w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/elseif-output-laravel-300x129.png 300w\" sizes=\"auto, (max-width: 323px) 100vw, 323px\" \/><\/figure><\/div>\n\n\n\n<h1 class=\"has-text-align-center has-vivid-red-color has-text-color wp-block-heading\">@isset Condition<\/h1>\n\n\n\n<p>@isset statement is used to check whether the variable we called is defined or not. If not defined then it&#8217;s not executed and prints next statement but doesn&#8217;t return an error. See the below example:- <\/p>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">Sample program<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"551\" height=\"186\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/isset-program-laravel.png\" alt=\"\" class=\"wp-image-11439\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/isset-program-laravel.png 551w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/isset-program-laravel-300x101.png 300w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/figure><\/div>\n\n\n\n<p>In the above program, Variable <strong>$string<\/strong> is not defined so @isset statement is not executed and the next statement prints. See the below output<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"335\" height=\"126\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/isset-output-laravel.png\" alt=\"\" class=\"wp-image-11442\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/isset-output-laravel.png 335w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/isset-output-laravel-300x113.png 300w\" sizes=\"auto, (max-width: 335px) 100vw, 335px\" \/><\/figure><\/div>\n\n\n\n<h1 class=\"has-text-align-center has-vivid-red-color has-text-color wp-block-heading\">@empty Condition<\/h1>\n\n\n\n<p>The @empty statement is used to check whether the called variable has value or not. If no value is assigned to the variable then statement executed otherwise not executed. See the below example:- <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"437\" height=\"119\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/no-value-laravel.png\" alt=\"\" class=\"wp-image-11471\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/no-value-laravel.png 437w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/no-value-laravel-300x82.png 300w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">Sample Program<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"244\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/empty-condition-laravel.png\" alt=\"\" class=\"wp-image-11459\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/empty-condition-laravel.png 544w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/empty-condition-laravel-300x135.png 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/figure><\/div>\n\n\n\n<p>In the above program, Variable <strong>$name<\/strong> has no value, so when we check <strong>$name<\/strong> then it returns statement written under @empty condition.<\/p>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">Output<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"328\" height=\"133\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/empty-condition-output-laravel.png\" alt=\"\" class=\"wp-image-11463\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/empty-condition-output-laravel.png 328w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/empty-condition-output-laravel-300x122.png 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/figure><\/div>\n\n\n\n<h1 class=\"has-text-align-center has-vivid-red-color has-text-color wp-block-heading\">@unless Condition<\/h1>\n\n\n\n<p>It is used to check whether the array or variable we called has any value declared or not.  If no value is assigned to the variable or array then statement executed otherwise not executed. See the below example:- <\/p>\n\n\n\n<p>  <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"437\" height=\"119\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/no-value-laravel.png\" alt=\"\" class=\"wp-image-11471\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/no-value-laravel.png 437w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/no-value-laravel-300x82.png 300w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">Sample Program <\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"488\" height=\"246\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/unless-condition-laravel.png\" alt=\"\" class=\"wp-image-11475\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/unless-condition-laravel.png 488w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/unless-condition-laravel-300x151.png 300w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/figure><\/div>\n\n\n\n<p>In the above program, there is no value assigned to the variable <strong>$name<\/strong> so it returns <strong>Value is empty.<\/strong> See the output below:- <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"336\" height=\"142\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/unless-condition-output-laravel.png\" alt=\"\" class=\"wp-image-11481\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/unless-condition-output-laravel.png 336w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/unless-condition-output-laravel-300x127.png 300w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><\/figure><\/div>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_88897\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_22963\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_88897\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>First of all, we make a condition.blade.php file in which we define our conditions. See the below image. After that, we make a function named condition in the controller and Defined a variable $name and assigned a string Sushant into it. After that returned it to view(condition.blade.php file is in a test folder). See the&#8230;<\/p>\n","protected":false},"author":19,"featured_media":11484,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5201],"tags":[5203],"class_list":["post-11381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-laravel"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/11381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=11381"}],"version-history":[{"count":59,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/11381\/revisions"}],"predecessor-version":[{"id":24902,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/11381\/revisions\/24902"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/11484"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=11381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=11381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=11381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}