10-Apr-2023
.
Admin
Hi Guys
This post will give you example of multiselect dropdown with checkbox in laravel . Here you will learn laravel multi select dropdown with checkbox. if you want to see example of multiselect dropdown with checkbox using laravel then you are a right place. This post will give you simple example of multiple select with checkbox example in laravel . You just need to some step to done laravel tutorial for multiselect dropdown with checkbox.
In this blog, I will explain how to multiselect dropdown with checkbox in laravel 7/6.we are going to discuss how to create multiple select dropdown listbox with checkbox select option by using Bootstrap Multiselect plugin. In one of the previous post we have already made discussion on multiple select dropdown list by using Bootstrap plugin, but in that plugin there is no functionality of checkbox option select.If you have use this plugin for make multiple select dropdown list then for select multiple option there is checkbox, so by check checkbox we can select multiple option from dropdown list box.
Here we will seen simple example with this type of multi select dropdown list box. We will insert value of multi select dropdown by using Ajax JQuery with Laravel 6.
Step 1 : Install Laravel 7/6 Application
we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Database Configuration
In this step, we require to make database configuration, you have to add following details on your .env file.
1.Database Username
1.Database Password
1.Database Name
In .env file also available host and port details, you can configure all details as in your system, So you can put like as bellow:
following path: .env
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Step 2: Create frameworks Table and Model
In this step we have to create migration for frameworks table using Laravel 7/6 php artisan command, so first fire bellow command:
php artisan make:model Framework -m
After this command you have to put bellow code in your migration file for create frameworks table.
following path: /database/migrations/2020_01_10_102325_create_frameworks_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFrameworksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('frameworks', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('frameworks');
}
}
Now we require to run migration be bellow command:
php artisan migrate
After you have to put bellow code in your model file for create frameworks table
following path:/app/Framework.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Framework extends Model
{
protected $fillable = [
'name'
];
}
Step 3: Create Route
In this is step we need to create route for multiselect dropdown with checkbox layout file
Route::get('frameworks','FrameworkController@index');
Route::post('frameworks','FrameworkController@store')->name('frameworks.store');
Step 4: Create Controller
here this step now we should create new controller as FrameworkController,So run bellow command for generate new controller
php artisan make:controller FrameworkController
now this step, this controller will manage multiselect dropdown with checkbox layout bellow content in controller file.following fille path
following path:/app/Http/Controllers/FrameworkController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Framework;
class FrameworkController extends Controller
{
public function index()
{
return view('framework');
}
public function store(Request $request)
{
$input = $request->all();
$data = [];
$data['name'] = json_encode($input['name']);
Framework::create($data);
return response()->json(['success'=>'Success Fully Insert Recoreds']);
}
}
Step 5: Create Blade Files
In Last step, let's create framework.blade.php (resources/views/framework.blade.php) for layout and multiselect dropdown with checkbox and put following code
following path:/resources/views/framework.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Multiselect Dropdown With Checkbox In Laravel - nicesnippets.com </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" />
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body class="bg-info">
<br /><br />
<div class="container" style="width:600px;">
<h2 align="center">Multiselect Dropdown With Checkbox In Laravel- nicesnippets.com</h2>
<br /><br />
<form method="post" id="framework_form">
<div class="form-group">
<label>Select which Framework you have knowledge</label>
<select id="framework" name="name[]" multiple class="form-control" >
<option value="Codeigniter">Codeigniter</option>
<option value="CakePHP">CakePHP</option>
<option value="Laravel">Laravel</option>
<option value="YII">YII</option>
<option value="Zend">Zend</option>
<option value="Symfony">Symfony</option>
<option value="Phalcon">Phalcon</option>
<option value="Slim">Slim</option>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-info" name="submit" value="Submit" />
</div>
</form>
<br />
</div>
</body>
<script>
$(document).ready(function(){
$('#framework').multiselect({
nonSelectedText: 'Select Framework',
enableFiltering: true,
enableCaseInsensitiveFiltering: true,
buttonWidth:'400px'
});
$('#framework_form').on('submit', function(event){
event.preventDefault();
var form_data = $(this).serialize();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url:"{{ route('frameworks.store') }}",
method:"POST",
data:form_data,
success:function(data)
{
$('#framework option:selected').each(function(){
$(this).prop('selected', false);
});
$('#framework').multiselect('refresh');
alert(data['success']);
}
});
});
});
</script>
</html>
Now we are ready to run our example so run bellow command for quick run:
php artisan serve
Now you can open bellow URL on your browser:
http://localhost:8000/frameworks
It Will help you...
#Laravel 7
#Laravel
#Laravel 6