Support the ongoing development of Laravel.io →
Database Validation

i have a input fields to insert names to the database at the moment it works like every time i enter one name it inserts and does not allow duplications the name but i want to enter multiple names comma separated this si what i have at the moment

front end

<form method="POST" action="{{route('store.names')}}">
            @csrf
            <div class="form-group row">
                <label for="names"
                       class="col-md-4 col-form-label text-md-right">Add New Name
                </label>

                <div class="col-md-6">
                    <input id="names" type="text"
                           class="form-control @error('names') is-invalid @enderror" name="names"
                           value="{{ old('names') }}" autocomplete="names" autofocus>

                    @error('names')
                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                    @enderror
                </div>
            </div>
    <div class="form-group row mb-0">
                <div class="col-md-6 offset-md-4">
                    <button type="submit" class="btn btn-primary">
                      Add Names
                    </button>
                </div>
            </div>
        </form>

Controller

    public function store(Request $request)
    {
      
        $validation = Names::create($this->validateRequest());

        return back()->with('message','Added'));
    }

   private function validateRequest()
    {
        return request()->validate([
            'names' => 'required|min:2|unique:Names',
        ]);
    }

enter image description here what this does is it inserts names of one person at a time how can i insert multiple people with comma separated with the validations intact

but this iss what i need enter image description here

i have figured it needs to be somthing like this

    $searchString = ',';

    if( strpos($request->names;, $searchString) !== false )
    {
      // insert in to db separately 
    }else{
        $names= Names::create($this->validateRequest());
    }
Last updated 3 years ago.
0


$arrayNames = explode(',', $request->input('names');
foreach($arrayNames as $name){
		Names::create($name);
}

Work now for validate the names.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

kunaloceanic roxozur Joined 29 Aug 2019

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.