In this article, we will see how to use the switch case statement in the blade/view or in the controller file in Laravel with an Example. Let’s

Laravel: Switch Case Statement In Controller Example

submited by
Style Pass
2021-06-13 10:30:05

In this article, we will see how to use the switch case statement in the blade/view or in the controller file in Laravel with an Example. Let’s just jump into it.

Switch statement is used to check the condition of many blocks one by one and it will execute the matched block. If no matches found then it will execute the default block and break the statement.

You can see in the above example, we are passing the status (publish, draft, trash) of post from the controller. Then in the blade file, we have used the switch case statement which will execute the publish case and then break the statement.

Let’s take a simple example for a better understanding. Let’s say we have a PostController and in that, we need to create a new post or update the post as per the form submission.

To do that we are passing status(save, update) variable while form submission and by using switch case statement we are checking the condition to perform save or update based on the condition matched.

Leave a Comment