So far I know how to populate dropdown using Eloquent (and pluck) is this
public function create()
{
$items = Item::all()->pluck('name', 'id');
return view('admin.item.create', compact('items'));
}
And with this
{!! Form::select('item_id', $items, null, ['class' => 'form-control']) !!}
I can generate this
<select name="items" class="form-control">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
But now I want to create something like this
<select name="items" class="form-control">
<option value="1">Item 1 - Location</option>
<option value="2">Item 2 - Location</option>
<option value="3">Item 3 - Location</option>
</select>
I can only use pluck with 2 attributes. But now I want it to display 3 attributes. How do I do that? I'm using Laravel 5.3 by the way. Thanks for the help.
You have an array like [1=> "something",2=>"else"]. So you task is to either iterate the array and edit the values or select the the locations from the database and the use foreach or array_filter
So something like the following:
$items=[];
foreach(Item::all() as $item){
$items[$item->id] = $item->name . " - " . $item->location->name; // build string to display here
}
Edit:
Your comment said location is at the same table so it would look like...
$items=[];
foreach(Item::all() as $item){
$items[$item->id] = $item->name . " - " . $item->location; // build string to display here
}
Pluck location also
public function create()
{
$items = Item::all()->pluck('name', 'id', 'location');
return view('admin.item.create', compact('items'));
}
Generate like this
<select name="items" class="form-control">
#foreach($items as $item)
<option value="{{ $item->id }}">{{ $item->name }} - {{ $item->location }}</option>
#endforeach
</select>
Best way to create list of array is.
public function create(){
$items = Item::where('id','<>','')->lists('name', 'id');
return view('admin.item.create', compact('items'));}
Related
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
I am working on a Laravel project, which should be somewhat modular.
I have written a search query and what I want is following:
Models:
Assessment A
Assessment B
Assessment C
View:
{{ Form::open(array('route' => 'search')) }}
<select multiple name="search_by_form[]">
<option selected>Any</option>
<option value="assessment_a">A</option>
<option value="assessment_b">B</option>
<option value="assessment_c">C</option>
</select>
{{ Form::close() }}
Controller:
The controller is much more complex but I made it easier for you to understand what my need is.
Now something like follows:
$user_entry =$request->input('search_by_form', []);
$resultArray = [];
foreach ($user_entry as $entry){
if ($entry == "assessment_a"){
$model_results = AssessmentA::where("approved",1)->get();
} elseif ($entry == "assessment_b"){
$model_results = AssessmentB::where("approved",1)->get();
} elseif($entry == "assessment_c"){
$model_results = AssessmentC::where("approved",1)->get();
}
}
What I want:
SearchViewController:
foreach through Models in specific folder
then:
{{ Form::open(array('route' => 'search')) }}
<select multiple name="search_by_form[]">
<option selected>Any</option>
#foreach($models as $model)
<option value="{{ $model }}">{{ $model->name }}</option>
#endforeach
</select>
{{ Form::close() }}
Instead of having such an ugly approach is it possible to have more something like this:
foreach ($user_entry as $entry){
$entry::where("approved",1)->get(); #Obviously the values on the view would be change to "AssessmentA", etc.
}
I this feasible?
Thank in advance!
I think you will create any array like this:
$model_array = ['assessment_a' => AssessmentA::class];
Then in loop simply pass value
foreach ($user_entry as $entry){
$model_array[$entry]::where("approved",1)->get(); #Obviously the values on the view would be change to "AssessmentA", etc.
}
If you change the select values to the actual table names, you could do it easily
{{ Form::open(array('route' => 'search')) }}
<select multiple name="search_by_form[]">
<option selected>Any</option>
<option value="assessments_a">A</option>
<option value="assessments_b">B</option>
<option value="assessments_c">C</option>
</select>
{{ Form::close() }}
$user_entry = $request->input('search_by_form', []);
if (empty($user_entry)) {
model_results = collect();
} else {
$query = DB::table(array_shift($user_entry))->where('approved', 1);
foreach ($user_entry as $table) {
$query->union(fn($union) => $union->from($table)->where('approved', 1));
}
$model_results = $query->get();
}
I'm trying to create a form with a select box in Laravel.
View
{{ Form::select('cmp_type', $cmp_types, null, $attributes = ['class' => 'form-control']) }}
Controller
public function showAddCompany()
{
$cmp_types = cmpTypes::where('status', true)->pluck('type');
return view('addCompany', compact('cmp_types'));
}
This is generating a select box.
<select class="form-control" id="cmp_type" name="cmp_type">
<option value="0">public</option>
<option value="1">pvt Ltd</option>
<option value="2">LLP</option>
<option value="3">NPO</option>
<option value="4">partnership</option>
<option value="5">proprietorship</option>
<option value="6">one person</option>
</select>
What can I do to get the select box as?:
<select class="form-control" id="cmp_type" name="cmp_type">
<option value="public">public</option>
<option value="pvt Ltd">pvt Ltd</option>
<option value="LLP">LLP</option>
<option value="NPO">NPO</option>
<option value="partnership">partnership</option>
<option value="proprietorship">proprietorship</option>
<option value="one person">one person</option>
</select>
I believe what is happening is that when you call pluck, you are being returned a numeric array (Collection?) of the values and those numeric keys are being used for the value property and the actual value of said key is being used as the display, what if you did:
$cmp_types = cmpTypes::where('status' , true )->pluck('type');
$cmp_types = array_combine($cpm_types, $cmp_types);
Reading Material
array_combine
array_combine() helped me.
the solution is:
public function showAddCompany()
{
$cmp_types_obj = cmpTypes::where('status' , true )->pluck('type');
$cmp_types = json_decode($cmp_types_obj);
$cmp_types = array_combine($cmp_types, $cmp_types);
return view('addCompany', compact('cmp_types'));
}
my query is returning an object, so i converted it into an array first and then i combined it with itself to get an associate array as
['public']=>'public,['private']=>'private',...
I want to get the Value of it to insert it into my Games table in database. But I have to show the name of genre from the Genres table in database.
View:
<select name="genreid">
#foreach($genres as $genre)
<option value="{{ $genre->genreid }}"> {{ $genre->genre }}</option>
#endforeach
</select>
Controller:
public function insertgame(Request $request){
$this -> validate($request, array(
'gamename' => 'required|min:3',
'price' => 'required|int|min:1',
'genre' => 'required',
'releaseddate' => 'required|date',
'picture' => 'required|mimes:jpeg,jpg,png,gif'
));
$gamename = $request->input('gamename');
$genreid = $request->input('genreid');
$price = $request->input('price');
$releaseddate = Carbon::parse($request->input('releaseddate'));
$picture = $request->file('picture')->getClientOriginalName();
$data=array('gamename' => $gamename, 'genreid'=>$genreid, 'price'=>$price,'releaseddate'=>$releaseddate,'picture'=>$picture );
DB::table('games')->insert($data);
return redirect('managegame');
}
You've said it works for you if you use this HTML:
<select name="genre">
<option selected disabled>Choose one</option>
<option value="FPS">FPS</option>
<option value="Action">Sports</option>
<option value="Action">Action</option>
<option value="Racing">Racing</option>
<option value="Simulation">Simulation</option>
</select>
In this case, to use proper data format, change this:
<option value="{{ $genre->genreid }}">{{ $genre->genre }}</option>
To:
<option value="{{ $genre->genre }}">{{ $genre->genre }}</option>
Do something like this
#foreach($game->genres as $genre)
{
<option value='{{$genre->id}}' {{$game->genre ==$genre->id ? 'selected' :' '> {{$genre->name}} </option>
}
I've found the answer!
I just changed
'genre' => 'required',
to
'genreid' => 'integer|required',
LOL Thanks all
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>