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',
]);
}
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
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());
}
$arrayNames = explode(',', $request->input('names');
foreach($arrayNames as $name){
Names::create($name);
}
Work now for validate the names.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community