Laravel store multiple select values to database example, In this tutorial we we will show you how to store multiple select values in laravel application. This step by step guide on store multiple selected selectbox options values in laravel.
Step 1: Create Model and Run Migration
Run the below command and generate model and migration:
php artisan make:model -m
Here is the migration file add title, description and category wich datatype should be string, text or json as per your requirement.
database\migrations\2021_09_26_072241_create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description');
$table->json('category');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Now update the fillable property in your model and set and get attribute for save in json and get in json_decode format your category.
app\Models\Post.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; protected $fillable = [ 'title', 'description', 'category' ]; /** * Set the categories * */ public function setCategoryAttribute($value) { $this->attributes['category'] = json_encode($value); } /** * Get the categories * */ public function getCatgoryAttribute($value) { return json_decode($value); } }
Now create two routes one for showing form and another is saving the data in database.
Step 2: Create Routes
Next, open the routes\web.php file and define your routes;
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('posts', [PostController::class, 'index']);
Route::post('posts', [PostController::class, 'store'])->name('posts.store');
Step 3: Create Controller
Here you need to create a controller run the below command.
php artisan make:controller PostController
Now put the below code on app\Http\Controllers\PostController.php file;
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
return view('posts');
}
public function store(Request $request){
Post::create($request->all());
return redirect()->back();
}
}
Step 5: Create Blade File
Next navigate resources/views and create blade file named as posts.blade.php. Now open resources\views\posts.blade.php and put the following code on it;
<!DOCTYPE html>
<html>
<head>
<title>How To Store Multiple Select Value In Laravel ? - codingdriver.com</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 offset-2 mt-5">
<div class="card">
<div class="card-header bg-info">
<h6 class="text-white">How to Store Multiple Select Values in Database using Laravel? - codingdriver.com</h6>
</div>
<div class="card-body">
<form method="post" action="{{ route('posts.store') }}">
@csrf
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control"/>
</div>
<div class="form-group">
<label><strong>Description :</strong></label>
<textarea class="form-control" name="description"></textarea>
</div>
<label>Select Category</label>
<select class="form-control" multiple="multiple" name="category[]">
<option value="laravel">Laravel</option>
<option value="jQuery">jQuery</option>
<option value="react">React</option>
<option value="vuejs">VueJs</option>
<option value="ajax">Ajax</option>
<option value="wordpress">WordPress</option>
</select>
<br>
<br>
<input type="submit" name="btn btn-primary" value="Save">
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You can run application using bellow command:
php artisan serve
I hope you like this article.