how To Fetch Checkbox Data in Laravel | Getting Checkbox Value in Laravel
If you want to know how to fetch checkbox data in Laravel , Then this article is for your . Here we will see a practical example of retriving checkbox data in string format .
As everybody knows whenever we retriving data from forms we need to add a name attribute to that specific field like ( for email field name=" email " ) . Similarly while retriving data from the checkbox , we also need to provide a name attribute but in the format of array ( e.g ; name=" checkbox[] " ) otherwise it will show you error .
In this example we will retrive data of multiple checkboxes which is possible if we provide their name attribute in array format and convert them to string using implode function of PHP and believe me it's very easy . We just need to add some codes in our controller .
Resources \ Views \ Welcome.blade.php ( Source Code ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> <!-- Styles --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script> </head> <body> <div class="my-5 container"> <center> <h1>Retrividng checkbox data in Laravel</h1> </center> </div> <div class="container mt-5"> <form action="{{ route('check') }}" method="post"> {{ csrf_field() }} <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"> <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small> </div> <div class="form-group"> <label for="exampleInputPassword1">Password</label> <input type="password" class="form-control" id="exampleInputPassword1"> </div> <div class="form-group form-check"> <input type="checkbox" class="form-check-input" id="exampleCheck1" name="check[]" value="cricket"> <label class="form-check-label" for="exampleCheck1">cricket</label> </div> <div class="form-group form-check"> <input type="checkbox" class="form-check-input" id="exampleCheck1" name="check[]" value="football"> <label class="form-check-label" for="exampleCheck1">football</label> </div> <div class="form-group form-check"> <input type="checkbox" class="form-check-input" id="exampleCheck1" name="check[]" value="tennis"> <label class="form-check-label" for="exampleCheck1">Tennis</label> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </body> </html> |
App\Http\Controllers\checkController.php ( Source Code ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; class checkController extends Controller { public function check(Request $req) { $all=$req->all(); $arraytostring=implode(',',$req->input('check')); $all['check']=$arraytostring; echo $all['check']; } } |
Output :
Thank you for reading this article 😊
Hope it helped you 😊