form select on laravel - php

I have data look like this
id rel word
1 A word A
2 B word B
3 B word C
to get the data my controller look like this :
public function get_new()
{
return View::make('form.new')
->with('datas', Data::all());
}
and my View will look like this :
<select>
#foreach($datas as $data)
<option value="{{ $data->id }}" rel="{{ $data->rel }}">{{ $data->word }}</option>
#endforeach
</select>
How i can get that data with Form::select ?
If we use Form::select we can call that with Data::lists('word','id') and pass to the view.
but if i use this i just can get 2 data that is id and word.
how can i get all data that is id, rel and word with Form::select. Please help me.

Laravel 4s FormBuilder and HtmlBuilder aren't going to produce everything for everyone. Taylor himself has said that he wants to keep it lean and simple. The solution you have now is probably the best way you could go about it (using a simple loop).
Form::select isn't capable of automatically populating other attributes of the option element. If you want this functionality you're going to either have to make yourself a custom macro or write your own form generator.
Probably not the answer you're after but that pretty much sums it up.

Just want to update the solution...
<select name="name" id="name">
#foreach($datas as $data)
<option value="{{ $data->id }}" {{ (Input::old('name', 0) === $data->id ? ' selected="selected"' : '') }} rel="{{ $data->rel }}">{{ $data->word }}</option>
#endforeach
</select>
This will give old input on selected option

Related

Laravel PHP 8 blade -> getting a list of all parent table records, list them in a select element and selecting the record->name based on the foreignID

So to break that title down...
I have 2 tables -> bosses (parent table) & employees (child)
bosses hasMany employees and employees hasOne boss
What I need to accomplish is to list all the bosses in an element and "select" the current boss. And if I change the boss from the list, it should change the id of the element to the new selection.
#foreach ($bosses as $boss)
<option value="{{ $boss->id }}" {{ $boss->id == ($employee->boss_id) ? 'selected' : '' }}>
{{ $boss->full_name }}
</option>
#endforeach
Controller
public function details($id){
$employee = Employee::find($id);
$bosses = Boss::get();
return view('employees.details',[
'employee' => $employee,
'bosses' => $bosses,
] );
}
Right now I'm still setting everything up so the search isn't working.
With the above code I'm getting all of the $publishers listed by name but it is not selecting the the boss associated the the boss_id foreign key.
Thanks in advance!!!
You could use the helper method old() for this.
For example:
<select name="boss_id>
#foreach ($bosses as $boss)
<option value="{{ $boss->id }}" {{ (old('boss_id', $boss->id) == $employee->boss_id) ? 'selected' : '' }}>
More info: https://laravel.com/docs/9.x/validation#repopulating-forms

Laravel, can't build query for array in multiple <select>

I made a filter to find businesses
There are several cities to choose from
Cities store on city_id column
The filter looks like this
<select id="city" multiple name="city[]">
#foreach($cities as $city)
<option value="{{ $city->name }}">{{ $city->name }}</option>
#endforeach
</select>
All request
dd($request->all())
shows me this
I build query for franchise or business
like this
if ($request->has('fb')) {
$businessesQuery->where('fb', $request->fb);
}
I try to build query like this but it's not works
if ($request->has('city[]')) {
$typeArray = explode(",", $request->city[]);
$businessesQuery->whereIn('city_id', $typeArray);
}
Help me solve this issue, I would be very grateful!
Your html must use city_id as a value and not the city name. Because you're trying to search using the id not name.
<option value="{{ $city->id }}">{{ $city->name }}</option>
explode function takes a string as in input and returns an array. In your case your city[] request value is already an array visible from the dump. You should use it directly. Something like this.
if ($request->has('city')) {
$businessesQuery->whereIn('city_id', $request->input('city');
}

Laravel slective Relationship

I am just building my first Laravel App and have the following question:
Simplified Table Structure:
Table CORPORATE
id pk
name
checkbox "isSupplier"
checkbox "isManufacturer"
Table COMPONENT
id pk
name
if_corporate fk (where isManufacturer=true)
So my question is, where do I need to put hands on the code to get this "selector" implemented?
Thanks
edit:
resources/views/admin/components/create.blade.php
<select class="form-control select2 {{ $errors->has('corp') ? 'is-invalid' : '' }}" name="corp_id" id="corp_id" required>
#foreach($corps as $id => $corp)
<option value="{{ $id }}" {{ old('corp_id') == $id ? 'selected' : '' }}>{{ $corp }}</option>
#endforeach
</select>
is this the correct place to modify?
You are already too late in the code you have shared. That is the view, which comes after the controller in this instance.
The controller is probably called something like ComponentsController there will be a line of code that says
$corps = Corporates::all();
Or something like that. That is where you need to update it. Probably something like this:
$corps = Corporates::where(["isManufacturer" => true)->get();
Now $corps will be a collection of only ones that are manufacturers.

How to pass more than id and name through laravel's lists()

I need to pass more than "name" and "id" to select box.
my model has id, name and price.
So I pass it to the view as:
$parts = Part::all()->lists('name','id);
I'd like to have select box options like:
<option value='id' data-price='price'>name</option>
My guess is try to pass array as first parameter in lists() method, but then I don't know is there way to use Form helper.
$parts = Part::all()->lists('["name"=>name, "price"=>price]','id');
Any suggestions?
Try to do something like
$parts = Part::all()->get(array('id', 'name', 'price'))->toArray();
that should give you only the wanted columns in an associative array :)
lists() is not for building a select, it just creates an array out of a collection. You have to pass the full model to the view and then build the select manually:
<select name="part">
#foreach($parts as $part)
<option value="{{ $part->id }}" data-price="{{ $part->price }}">
{{ $part->name }}
</option>
#endforeach
</select>

Select drop down from table array

Here is part of my controller;
$agreements = Agreement::all();
print_r($agreements) //this works and displays an object/array with the details!
return View::make('individual_agreements.create')
->with('individual', $individual)
->with('agreements', $agreements)
->with('content_title', 'Create new individual agreement');
Here is part of my view;
<div class="form-group">
{{ Form::label('agreement_name', 'Agreement name') }}
{{ Form::select('agreement_name', $agreements->agreement_name) }}
</div>
I have a field called agreement_name in the table.. All I want to do is turn that in to a dropdown in the most efficient Laravel/Eloquent way as possible.
I keep getting the standard "agreement_name not defined" error and I cannot find a suitable example anywhere online having looked for over an hour now.
If you need the agreements for something else on the same page try
<div class="form-group">
{{ Form::label('agreement_name', 'Agreement name') }}
{{ Form::select('agreement_name', $agreements->lists('agreement_name', 'agreement_name')) }}
</div>
This will get all the agreements names within your agreements collection and allow them to be used within the select drop down. First parameter is the column and second is the key.
Else just change your query to;
$agreements = Agreement::lists('agreement_name', 'id');
You should do this
$agreements = Agreement::lists('agreement_name', 'id');
all return you all columns in table, so that's why your code wont work
The way you have this set up allows for the use of a foreach loop to list through and display results:
<select name="agreement_name" class="">
<option value="">- Select -</option>
#foreach($agreements AS $agreement)
<option value="{{ $agreement->id }}">{{ $agreement->agreement_name }}</option>
#endforeach
</select>
Since your $agreements is an array of elements, calling $agreements->agreement_name wouldn't work, while $agreements[0]->agreement_name probably would. If you want to use the Form::select() option, you'll need to use a different method of getting your agreements. Check the other answers for that solution.
Hope this provided some insight!
Edit
I should note that this isn't the Laravel Way of doing things, but it is certainly viable.

Categories