Laravel Form with multiple select












0















I have the following controller, which sends data to a Laravel Blade view:



Controller:



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


In that Laravel blade view there is a form:



<form method="GET" action="{{ route('invoices.choose-periods') }}">
<div class="form-group {{ $errors->has('school') ? 'has-error' : '' }}">
<label>School</label>
<select id="school" class="form-control" name="school" multiple size="{{ $schools->count() }}" required>
@foreach ($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
@endforeach
</select>
@if ($errors->has('school'))
<span class="help-block">
<strong>{{ $errors->first('school') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-success btn-sm pull-right">Submit</button>
</form>


As you can see from the HTML, the form is a multi-select form, with the resulting data stored in a school array.



On submission of the form, I do a test die and dump on request('school') and see that for every option I have selected, the value seems to have been logged twice. For example, choosing only one option gives me:



array:2 [▼
0 => "15"
1 => "15"
]


Any ideas? Thanks!










share|improve this question

























  • the problem doesn't seems related to your blade template

    – Alessandro
    Jun 23 '17 at 15:11













  • I agree. Baffled...

    – Ows
    Jun 23 '17 at 15:13











  • Are you sure you have single selector in your view ? Others seems fine

    – Sagar Gautam
    Jun 23 '17 at 16:37






  • 1





    Sorry by event x i mean how the data is stored from clicking an item in the multiple select. Anything special going on in route('invoices.choose-periods')? Also check if your blade does not contain name="school" twice.

    – Lars Mertens
    Jun 23 '17 at 16:59






  • 1





    Offtopic: would recommend to work with Laravel Collective if that's possible laravelcollective.com/docs/5.4/html. When generating this form you might have no _token on it which can cause security problems in the future.

    – Lars Mertens
    Jun 23 '17 at 17:13
















0















I have the following controller, which sends data to a Laravel Blade view:



Controller:



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


In that Laravel blade view there is a form:



<form method="GET" action="{{ route('invoices.choose-periods') }}">
<div class="form-group {{ $errors->has('school') ? 'has-error' : '' }}">
<label>School</label>
<select id="school" class="form-control" name="school" multiple size="{{ $schools->count() }}" required>
@foreach ($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
@endforeach
</select>
@if ($errors->has('school'))
<span class="help-block">
<strong>{{ $errors->first('school') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-success btn-sm pull-right">Submit</button>
</form>


As you can see from the HTML, the form is a multi-select form, with the resulting data stored in a school array.



On submission of the form, I do a test die and dump on request('school') and see that for every option I have selected, the value seems to have been logged twice. For example, choosing only one option gives me:



array:2 [▼
0 => "15"
1 => "15"
]


Any ideas? Thanks!










share|improve this question

























  • the problem doesn't seems related to your blade template

    – Alessandro
    Jun 23 '17 at 15:11













  • I agree. Baffled...

    – Ows
    Jun 23 '17 at 15:13











  • Are you sure you have single selector in your view ? Others seems fine

    – Sagar Gautam
    Jun 23 '17 at 16:37






  • 1





    Sorry by event x i mean how the data is stored from clicking an item in the multiple select. Anything special going on in route('invoices.choose-periods')? Also check if your blade does not contain name="school" twice.

    – Lars Mertens
    Jun 23 '17 at 16:59






  • 1





    Offtopic: would recommend to work with Laravel Collective if that's possible laravelcollective.com/docs/5.4/html. When generating this form you might have no _token on it which can cause security problems in the future.

    – Lars Mertens
    Jun 23 '17 at 17:13














0












0








0








I have the following controller, which sends data to a Laravel Blade view:



Controller:



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


In that Laravel blade view there is a form:



<form method="GET" action="{{ route('invoices.choose-periods') }}">
<div class="form-group {{ $errors->has('school') ? 'has-error' : '' }}">
<label>School</label>
<select id="school" class="form-control" name="school" multiple size="{{ $schools->count() }}" required>
@foreach ($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
@endforeach
</select>
@if ($errors->has('school'))
<span class="help-block">
<strong>{{ $errors->first('school') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-success btn-sm pull-right">Submit</button>
</form>


As you can see from the HTML, the form is a multi-select form, with the resulting data stored in a school array.



On submission of the form, I do a test die and dump on request('school') and see that for every option I have selected, the value seems to have been logged twice. For example, choosing only one option gives me:



array:2 [▼
0 => "15"
1 => "15"
]


Any ideas? Thanks!










share|improve this question
















I have the following controller, which sends data to a Laravel Blade view:



Controller:



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


In that Laravel blade view there is a form:



<form method="GET" action="{{ route('invoices.choose-periods') }}">
<div class="form-group {{ $errors->has('school') ? 'has-error' : '' }}">
<label>School</label>
<select id="school" class="form-control" name="school" multiple size="{{ $schools->count() }}" required>
@foreach ($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
@endforeach
</select>
@if ($errors->has('school'))
<span class="help-block">
<strong>{{ $errors->first('school') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-success btn-sm pull-right">Submit</button>
</form>


As you can see from the HTML, the form is a multi-select form, with the resulting data stored in a school array.



On submission of the form, I do a test die and dump on request('school') and see that for every option I have selected, the value seems to have been logged twice. For example, choosing only one option gives me:



array:2 [▼
0 => "15"
1 => "15"
]


Any ideas? Thanks!







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 23 '17 at 16:43









Webinion

1,72021226




1,72021226










asked Jun 23 '17 at 15:03









OwsOws

346




346













  • the problem doesn't seems related to your blade template

    – Alessandro
    Jun 23 '17 at 15:11













  • I agree. Baffled...

    – Ows
    Jun 23 '17 at 15:13











  • Are you sure you have single selector in your view ? Others seems fine

    – Sagar Gautam
    Jun 23 '17 at 16:37






  • 1





    Sorry by event x i mean how the data is stored from clicking an item in the multiple select. Anything special going on in route('invoices.choose-periods')? Also check if your blade does not contain name="school" twice.

    – Lars Mertens
    Jun 23 '17 at 16:59






  • 1





    Offtopic: would recommend to work with Laravel Collective if that's possible laravelcollective.com/docs/5.4/html. When generating this form you might have no _token on it which can cause security problems in the future.

    – Lars Mertens
    Jun 23 '17 at 17:13



















  • the problem doesn't seems related to your blade template

    – Alessandro
    Jun 23 '17 at 15:11













  • I agree. Baffled...

    – Ows
    Jun 23 '17 at 15:13











  • Are you sure you have single selector in your view ? Others seems fine

    – Sagar Gautam
    Jun 23 '17 at 16:37






  • 1





    Sorry by event x i mean how the data is stored from clicking an item in the multiple select. Anything special going on in route('invoices.choose-periods')? Also check if your blade does not contain name="school" twice.

    – Lars Mertens
    Jun 23 '17 at 16:59






  • 1





    Offtopic: would recommend to work with Laravel Collective if that's possible laravelcollective.com/docs/5.4/html. When generating this form you might have no _token on it which can cause security problems in the future.

    – Lars Mertens
    Jun 23 '17 at 17:13

















the problem doesn't seems related to your blade template

– Alessandro
Jun 23 '17 at 15:11







the problem doesn't seems related to your blade template

– Alessandro
Jun 23 '17 at 15:11















I agree. Baffled...

– Ows
Jun 23 '17 at 15:13





I agree. Baffled...

– Ows
Jun 23 '17 at 15:13













Are you sure you have single selector in your view ? Others seems fine

– Sagar Gautam
Jun 23 '17 at 16:37





Are you sure you have single selector in your view ? Others seems fine

– Sagar Gautam
Jun 23 '17 at 16:37




1




1





Sorry by event x i mean how the data is stored from clicking an item in the multiple select. Anything special going on in route('invoices.choose-periods')? Also check if your blade does not contain name="school" twice.

– Lars Mertens
Jun 23 '17 at 16:59





Sorry by event x i mean how the data is stored from clicking an item in the multiple select. Anything special going on in route('invoices.choose-periods')? Also check if your blade does not contain name="school" twice.

– Lars Mertens
Jun 23 '17 at 16:59




1




1





Offtopic: would recommend to work with Laravel Collective if that's possible laravelcollective.com/docs/5.4/html. When generating this form you might have no _token on it which can cause security problems in the future.

– Lars Mertens
Jun 23 '17 at 17:13





Offtopic: would recommend to work with Laravel Collective if that's possible laravelcollective.com/docs/5.4/html. When generating this form you might have no _token on it which can cause security problems in the future.

– Lars Mertens
Jun 23 '17 at 17:13












1 Answer
1






active

oldest

votes


















0














I have only worked on laravel 5.7. Try this It is working for me.
Since you are passing 2 objects



return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);


It is obvious you will get 2 errors.



In your controller change this



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


to this



public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}





share|improve this answer
























  • Cheers. Thanks for your help.

    – Ows
    Nov 25 '18 at 21:25











  • is it working for you??

    – vinayak_shahdeo
    Nov 27 '18 at 16:45











  • Have not had time to check yet sorry!

    – Ows
    Nov 28 '18 at 22:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44724583%2flaravel-form-with-multiple-select%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I have only worked on laravel 5.7. Try this It is working for me.
Since you are passing 2 objects



return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);


It is obvious you will get 2 errors.



In your controller change this



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


to this



public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}





share|improve this answer
























  • Cheers. Thanks for your help.

    – Ows
    Nov 25 '18 at 21:25











  • is it working for you??

    – vinayak_shahdeo
    Nov 27 '18 at 16:45











  • Have not had time to check yet sorry!

    – Ows
    Nov 28 '18 at 22:46
















0














I have only worked on laravel 5.7. Try this It is working for me.
Since you are passing 2 objects



return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);


It is obvious you will get 2 errors.



In your controller change this



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


to this



public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}





share|improve this answer
























  • Cheers. Thanks for your help.

    – Ows
    Nov 25 '18 at 21:25











  • is it working for you??

    – vinayak_shahdeo
    Nov 27 '18 at 16:45











  • Have not had time to check yet sorry!

    – Ows
    Nov 28 '18 at 22:46














0












0








0







I have only worked on laravel 5.7. Try this It is working for me.
Since you are passing 2 objects



return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);


It is obvious you will get 2 errors.



In your controller change this



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


to this



public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}





share|improve this answer













I have only worked on laravel 5.7. Try this It is working for me.
Since you are passing 2 objects



return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);


It is obvious you will get 2 errors.



In your controller change this



public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}


to this



public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 8:36









vinayak_shahdeovinayak_shahdeo

204




204













  • Cheers. Thanks for your help.

    – Ows
    Nov 25 '18 at 21:25











  • is it working for you??

    – vinayak_shahdeo
    Nov 27 '18 at 16:45











  • Have not had time to check yet sorry!

    – Ows
    Nov 28 '18 at 22:46



















  • Cheers. Thanks for your help.

    – Ows
    Nov 25 '18 at 21:25











  • is it working for you??

    – vinayak_shahdeo
    Nov 27 '18 at 16:45











  • Have not had time to check yet sorry!

    – Ows
    Nov 28 '18 at 22:46

















Cheers. Thanks for your help.

– Ows
Nov 25 '18 at 21:25





Cheers. Thanks for your help.

– Ows
Nov 25 '18 at 21:25













is it working for you??

– vinayak_shahdeo
Nov 27 '18 at 16:45





is it working for you??

– vinayak_shahdeo
Nov 27 '18 at 16:45













Have not had time to check yet sorry!

– Ows
Nov 28 '18 at 22:46





Have not had time to check yet sorry!

– Ows
Nov 28 '18 at 22:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44724583%2flaravel-form-with-multiple-select%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python