Select drop down from table array - php

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.

Related

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: Variable not being passed from controller to view

using Laravel 5.6.28 my route on web.php file looks like this:
Route::get('kitysoftware/besttrades', 'SectorsController#besttradesview');
My controller with bestrasdeview function:
class SectorsController extends Controller
{
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
foreach ($sectors as $sector) {
echo $sector->SectorName;
}
//passing variable sectors1 to my view (is = $sector)
return view('besttradesview', ['sectors1' => $sector]);
}}
My view Bestradesview.blade.php is:
<form method="GET">
<div class="selectsector">
<Select class="selectsector" name = "sectors">
<option value="{{ sectors1 }}"></option>
<!-- test#2: <option value="{{ $sectors1->sector[] }}"></option> -->
<!-- test#3: <option value="{{ $sectors1->sector }}"></option> -->
</form>
</select>
And i get this error: Use of undefined constant sectors1 - assumed
'sectors1' (this will throw an Error in a future version of PHP)
When testing #2 commented line instead i get another error: Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Cannot use [] for reading
When testing #3 commented line without [] i get another error: Use of undefined constant sectors1 - assumed 'sectors1' (this will throw an Error in a future version of PHP)
Probably it's pretty simple but it's driving me nuts because i can't see why the variable is not passing to the view.
I know it's reading my table because if i remove the last return view call line on my controller, it echoes out all the values from my SectorName column, but i want it on a select dropdown menu.
I have been reading the docs, forums and watching laracast videos without luck. Any insight or just pointing me out to where to learn the proper sintax solution will be appreciatted.
Thanks in advance
It looks like you're trying to populate the select options from the query. If that's the case, your loop is in the wrong place.
Take the loop out of the controller method, and pass the $sectors collection directly to the view.
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
return view('besttradesview', ['sectors1' => $sectors]);
}
Then loop in the view to output the options.
<select class="selectsector" name="sectors">
#foreach($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
</select>
I assumed you wanted some text in the option. If you use the sector name as the option text it will also be used as the option value by default. If you want to use something else for the value, it would be like this, for example:
<option value="{{ $sector->id }}">{{ $sector->SectorName }}</option>
Change,
return view('besttradesview', ['sectors1' => $sector]);
To,
return view('besttradesview', ['sectors1' => $sectors]);
Notice the s in $sectors ($sectors = DB::table('Sectors')->get();) and in the actual view, it should be $sectors1 not just sectors1.
You also need to loop through actual sector:
#foreach ($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
Also, I would definitely suggest creating a model for your Sectors table rather than using the DB:: facade. You should make use of the M in MVC.

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>

Laravel/Blade Drop Down List - For more than one field

I'm trying to use blade to display a dropdown list from table data. The problem I have, is that I want to display the results of two fields in the table concatenated, not just one.
So I want something to render something like;
<select id="agreement_type" name="agreement_type">
<option value="1">Agreement Field 1 - Agreement Field 2</option>
<option value="2">Agreement Field 1 - Agreement Field 2</option>
<option value="4">Agreement Field 1 - Agreement Field 2</option>
</select>
My controller currently looks like this;
$agreements = Agreement::lists('agreement_type','id');
return View::make('client_agreements.create')
->with('agreements', $agreements);
My blade view currently looks like this;
<div class="form-group">
{{ Form::label('agreement_type', 'Agreement type') }}
{{ Form::select('agreement_type', $agreements) }}
</div>
I have tried to amend the view and controller in various ways to get the desired output. But can't get it to work.
I want to display agreement_type and level and have the id set as the value so I tried;
$agreements = Agreement::lists('agreement_type'.'level','id');
But this just displays level as the value and completely ignores id.
This is the simplest method. Just use a foreach loop to build the options array:
$agreements = Agreement::all();
$agreementOptions = array();
foreach($agreements as $agreement){
$agreementOptions[$agreement->id] = $agreement->agreement_type.' '.$agreement->level;
}
return View::make('client_agreements.create')
->with('agreements', $agreementOptions);
However you can also define an attribute accessor in your model. You can use that to create new properties that can be accessed normal, but you can run logic to generate them (like combining to attributes)
public function getSelectOptionAttribute(){
return $this->attributes['agreement_type'].' '.$this->attributes['level'];
}
And then you can use lists:
$agreements = Agreement::all()->lists('select_option', 'id');
(Laravel converts SelectOption (studly case) from the method name to select_option (snake case) for the attribute, so don't get confused by that)

form select on laravel

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

Categories