I'm trying to get to decode the following:
$options = Input::get('options');
$product->options = json_encode($options);
Within a select/option dropdown but when I use json_decode like so:
<select name="options">
<option>{{ $item->options }}</option>
</select>
(I'm Laravel blade here)
When I view it the browser I get the following image - http://d.pr/i/D8z7
It's also i a foreach loop too.
So what I'm I doing wrong or how can I achieve this ??
You have to loop it.
<select name="options">
#foreach ($item->options as $option)
<option>{{ $option }}</option>
#endforeach
</select>
Any idea's anyone, really need to get this sorted. it looks like when i json_encode thats fine i just need to return the json correctly into the select menu
Related
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');
}
I want to update the two records that I got by using the get() with different value.
I fetched the Auth user language with this line of code.
$languages = UserLanguage::where('user_ID', Auth::user()->id)->get();
this is the result of my diedump
In my view this is the foreach loop
<select name="language">
#foreach( $languages as $language )
<option value="{{ $language->id }}">{{ $language->language_name}}</option>
#endforeach
</select>
this is the view picture of foreach
I want to change those 2 records with English and Japanese with 1 button but I don't know how to update those records. please help
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>
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.
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