How To Store Multiple Select Value In Laravel ?

10-Mar-2023

.

Admin

How To Store Multiple Select Value In Laravel ?

Hi Dev,

In this article, I will show you multiple select value store into the database in laravel app. I want to store all select value into table in laravel.

I will give you full exaple for store multiple option into table in laravel. You need store multiple select value into table with our laravel app then you can use bellow full exmaple. So Let's see full example step by step.

Here i will give solution and full exmaple for store multiple value.

Solution


$hobby = $request->input('hobby');

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

Step 1 : Install Laravel App

In First step 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:

.env

DB_HOST=localhost

DB_DATABASE=homestead

DB_USERNAME=homestead

DB_PASSWORD=secret

Step 2: Create users Table and Model

In this step we have to create migration for users table using Laravel php artisan command, so first fire bellow command:

php artisan make:model User -m

After this command you have to put bellow code in your migration file for create users table.

/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');

}

}

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 users table.

app/User.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model

{

protected $fillable = [

'name','hobby'

];

}

Step 3 : Create Route

now, we need to add route for users controller in laravel application. so open your "routes/web.php" file and add following route.

Route::get('/users','UserController@create');

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

Step 4 : Create Controller

Here this step now we should create new controller as UserController. So run bellow command and create new controller.

php artisan make:controller UserController

After successfully run above command . So, let's copy bellow code and put on UserController.php file.

/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: 5 Create View

In last step. In this step we have to create "create" blade file. So mainly we have to create layout file . So finally you have to create following file and put bellow code:

/resources/views/create.blade.php

<!DOCTYPE html>

<html>

<head>

<title>How To Store Multiple Select Value In Laravel ? - NiceSnippets.com</title>

</head>

<body>

<h1>How To Store Multiple Select Value In Laravel ? Nicesnippets</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>

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/users

It will help you...

#Laravel 7

#Laravel

#Laravel 6