Home » Laravel: Switch Case Statement In Controller Example

Laravel: Switch Case Statement In Controller Example

Last updated on June 12, 2021 by

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.

Table of Contents
1. How Switch Case Statement Works
2. Example Laravel Blade Switch Case
3. Example Switch Case In Controller

How Switch Case Statement Works

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.

Let’s see the example of the Laravel blade switch case statement.

Example Laravel Blade Switch Case

Laravel switch statements can be constructed using the @switch, @case, @break, @default and @endswitch directives:

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function index()
    {
        $status = 'publish';
        return view('posts.post', compact('status'));
    }
}

resources/views/posts/post.blade.php

<!DOCTYPE html>
<html>
   <head>
      <title>Laravel Switch Case Statement Example | ScratchCode</title>
   </head>
   <body>
        @switch($status)
            @case('publish')
                <span class="status">Publish</span>
                @break

            @case('draft')
                <span class="status">Draft</span>
                @break

            @default
                <span class="status">Trash</span>
        @endswitch
   </body>
</html>

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.

Example Switch Case In Laravel Controller

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.

We need to use the same switch case statement as we are using in the PHP.

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use App\Requests\PostRequest;

class PostsController extends Controller
{
    public function store(PostRequest $request)
    {
        switch($request->status) {
            case('save'):

                $posts = Post::Create([
                    'name' => $request->name;
                    'description' => $request->description;
                    'status' => $request->status;
                ]);

                $msg = 'Post successfully saved.';

                break;

            case('update'):
                
                $posts = Post::find($request->id)->first();

                $post->name = $request->name;
                $post->description = $request->description;
                $post->status = $request->status;

                $post->save();

                $msg = 'Post successfully updated.';

                break;

            default:
                $msg = 'Something went wrong.';
        }

        return view('posts.post', compact('msg'));
    }
}

Additionally, read our guide:

  1. Laravel: Blade Switch Case Statement Example
  2. Laravel: Switch Case Statement In Controller Example
  3. Laravel: Change Column Type In Migration
  4. Laravel: Change Column Name In Migration
  5. How To Use Where Date Between In Laravel
  6. How To Add Laravel Next Prev Pagination
  7. Laravel Remove Column From Table In Migration
  8. Laravel: Get Month Name From Date
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Update Pivot Table In Laravel
  11. How To Dynamic iFrame URL In Elementor
  12. How To Handle Failed Jobs In Laravel
  13. How To Remove WooCommerce Data After Uninstall
  14. How To Get Latest Records In Laravel
  15. How To Break Nested Loops In PHP Or Laravel
  16. How To Pass Laravel URL Parameter
  17. Laravel Run Specific Migration
  18. How To Fix Elementor Icons Not Showing

That’s it from our end. We hope this article helped you to learn how to use the laravel switch case statement in the views/blade or in controllers file.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

1 thought on “Laravel: Switch Case Statement In Controller Example”

Leave a Comment