Laravel How to pass variable from select box to url? - php

What I am trying to do is basicly passing selected item's slug from a select box to route
{{ Form::open(['route' => 'list/{category->slug(here)}' ]) }}
<select name="" id="" class="form-control">
#foreach($categories $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
{{ Form::submit('Send') }}
{{ Form::close() }}
Can anyone help me?

You need to use some javascript (jQuery) to do this
$("#select").change(function() {
var option = $(this).val();
$("#form-id").attr("action", "list/" + option);
});

You can try to use the onchange. Like here:
<select onchange="document.location=this.options[this.selectedIndex].value">
<option value="{{ route('route_name') }}">caption</option>

Related

How to keep values in <select> after submitting a form in laravel 8

I have multiple <select> by city filter
select in blade
<select id="city" multiple name="city[]">
#foreach($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
Query for select
if ($request->has('industry')) {
$businessesQuery->whereIn('industry_id', $request->input('industry'));
}
try this
#if(request('type') != "")
$('#city').find('option[value="' + "{{request('type')}}" + '"]').attr('selected', true);
#endif
little manage according to your code then its should work

old multiselect array using select option in laravel 8

am trying to make old values which would be used when am editing but it's returning
htmlspecialchars(): Argument #1 ($string) must be of type string, array giveneverytime
but when i dd the variable i found that it has array but when it reaches the option it changes into error
this is my select in blade
{!! Form::label('functional_area_id', 'Functional Area', ['class' => 'bold']) !!}
<?php
$functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds);
?>
{{-- this codes works perfect but it doesn't display the relation between child and parent and i want that to be displayed
{!! Form::select('functional_area_id[]', $functionalAreas, $functionalAreaIds, array('class'=>'form-control select2-multiple', 'id'=>'functional_area_id','multiple'=>'multiple')) !!} --}}
<select name="functional_area_id[]" id="functional_area_id" class="form-control select2-multiple" multiple="multiple">
#foreach ($menulist as $category)
<option value={{ $category->functional_area_id }} {{ $functionalAreaIds }} >{{ $category->functional_area }}</option>
#if (count($category->children) > 0)
#include('submenu', ['submenu' => $category->children, 'parent' =>
$category->functional_area])
#endif
#endforeach
</select>
{!! APFrmErrHelp::showErrors($errors, 'functional_area_id') !!}
</div>
this is the controller which fetches the array
public function jobFunctionals()
{
return $this->hasMany('App\JobFunctionalAreas', 'job_id', 'id');
}
public function getJobFunctionalsArray()
{
return $this->jobFunctionals->pluck('functional_area_id')->toArray();
}
You can only output strings using {{ $variable }}, because Blade will run $variable through htmlspecialchars escaping, that's why you get an error trying to output {{ $functionalAreaIds }} which, judging by the name, is an array.
I have no idea what you are including with the submenu partial, but it is likely invalid HTML. The only tags that are allowed within a select element are option and optgroup tags. Optgroups need to be opened and closed around options, so I don't see how this would work in your setup.
Also, you have syntax errors in your option tag:
<option value={{ $category->functional_area_id }} {{ $functionalAreaIds }} >{{ $category->functional_area }}</option>
What do you expect this to do? This would - if it worked at all - result in something like this:
<option value=1 [1,5,6] >Area 1</option>
Which is clearly not valid HTML. What do you expect the array to do after every option value?
To fix the option part of your code, you can do something like this:
<option value="{{ $category->functional_area_id }}" #if(in_array(category->functional_area_id, $functionalAreaIds)) selected #endif >{{ $category->functional_area }}</option>
This will set all options selected that are within the $functionalAreaIds array.
Maybe you should get rid of the code including the submenu when testing, because this is likely to break your HTML too, and then take it from there.
Another tip:
<?php
$functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds);
?>
can be written in blade like this:
#php($functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds))

Laravel | custom collection for select2 optgroup and option

Using select 2 I want to do something like this (but this syntax is not quite right):
#foreach($courseCategories as $courseCategory)
<select>
#foreach($courseCategory->courseNames as $courseName)
<optgroup label="{{ $courseCategory }}">
<option value="{{ $courseName }}">
{{ $courseName }}
</option>
</optgroup>
</select>
#endforeach
My Model Structure is quite like this:
In my controller:
public function create()
{
$courses = Course::all();
return View::make('college.create', compact('courses'));
}
I need to map courseCategory => optgroup's label and respective course name => option
How can I create a custom collection to faciliate this? and How can I use the collection (I'm guessing using foreach) to output values in my view file?
There a problem in your foreach :
<select>
#foreach($courseCategories as $courseCategory)
#foreach($courseCategory->courseNames as $courseName)
<optgroup label="{{ $courseCategory }}">
<option value="{{ $courseName }}">
{{ $courseName }}
</option>
</optgroup>
#endforeach
#endforeach
</select>

laravel 5.4 form dropdown from database

I want to fetch drop down option value & name from database, to do that my controller code is
$roles = Role::pluck('role','user_id');
return view('users.add_role',compact('roles'));
to fetch this data from drop down list my view file code is
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role->user_id }} "> {{ $role->role }} </option>
#endforeach
</select>
but it says error
Trying to get property of non-object (View: C:\xampp\htdocs\auth2\resources\views\users\add_role.blade.php)
if i just only use
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role }} "> {{ $role }} </option>
#endforeach
</select>
then it shows role column of the table, but it not pass option value to database. so what is the correct way to generate option value & name from database?
Your $roles variable containt an array which is looks like
[0] => 'your_role'
[1] => 'your_role'
Where is 0 and 1 index is user_id. So your user_id is set as an index of your $roles array. Simply do it dd($roles) and check the output. $key as $role will work. Where $key will containt the user_id.
#foreach($roles as $key => $role)
<option value="{{ $key }} "> {{ $role }} </option>
#endforeach
I use laravel collective package to generate dropdown list much more easier,
you can do things like, on your blade template and it will render the list for you
{{ Form::select('role', $role )) }}

Laravel : Form select option value is always NULL when submitting

I have a form where I put de id's of my bands table inside a select option. When I select an id in the dropdownlist and try to submit the form, the value always remains NULL. How can I retrieve the value I selected?
create.blade.php file :
<form>
<select name="band_id">
#foreach ($bands as $band)
<option value="{{ $band->band_id }}">{{ $band->band_id }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
</form>
My Bandcontroller inside my store action :
$bands->band_id = $input['band_id'];
First of all you are using #foreach ($band as $band), make sure it's not a typo and if not then fix it (Could be $bands as $band and assumed you know what I mean). Anyways, you may also try this:
{{ Form::select('band_id', $bands, Input::old('band_id')) }}
Just pass the $bands variable to the View and Laravel will create the SELECT for you, you don't need to loop manually. To retrieve the value, you may try this:
$band_id = Input::get('band_id');
Laravel 6 or above
//blade
{!!Form::open(['action'=>'CategoryController#storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
#foreach ($cats as $cat)
<option value={{$cat->id}}>{{ $cat->title }}</option>
#endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
// controller
public function storecat(Request $request){
Log::channel('stack')->info('name'.$request->app);
if($request->input('cat')==null){ // retriving option value by the name attribute of select tag
return redirect(route('cats.cat'))->with('error', 'Select an app');
}
$this->validate($request,[
'name'=>'required',
]);
}

Categories