Hello Devs,

In this tutorial, we are going to learn how to store multiple select value into the database in laravel app.

Follow this step by step guide given below:


Solution

$hobby = $request->input('hobby');
$input['hobby'] = implode(',', $hobby);



Step 1 : 


Install Laravel App

composer create-project --prefer-dist laravel/laravel blog



Step 2 :

Database Configuration

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret



Step 3 : 


Create users Table and Model 

php artisan make:model User -m

/database/migrations/2020_05_26_100722_create_users_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('hobby');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Run this command:

php artisan migrate

app/User.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = [
        'name','hobby'
    ];
}



Step 4 : 


Create Route 

Route::get('/users','UserController@create');
Route::post('/users','UserController@store')->name('users.store');



Step 5 : 


Create Controller

php artisan make:controller UserController

/app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->all();
        $hobby = $input['hobby'];
        $input['hobby'] = implode(',', $hobby);
        
        User::create($input);
        return redirect()->back();
    }

}



Step 6 : 


Create View /resources/views/create.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How To Store Multiple Select Value In Laravel ? - Rathorji.in</title>
</head>
<body>
    <h1>How To Store Multiple Select Value In Laravel ? Rathorji</h1>
    <form method="post" action="{{ route('users.store') }}">
        @csrf
        <label>Name</label>
        <input type="text" name="name">
        <br>
        <br>
        <label>Hobby</label>
        <select multiple="multiple" name="hobby[]">
            <option>Cricket</option>
            <option>singing</option>
            <option>playing</option>
            <option>reading</option>
            <option>listening</option>
            <option>travelling</option>
        </select>
        <br>
        <br>
        <input type="submit" name="btn-submit" value="Save">
    </form>
</body>
</html>

Run this command:

php artisan serve

Open this URL:

http://localhost:8000/users


I hope this example helps you.