Is that possible to get data from other table?
Let say in create table, I get all the info from genre table and display all data in drop down list
<select name="mov_genre" id="mov_genre" class="form-control input-sm">
<option value="">类型</option>
#foreach(App\Genre::all() as $gData)
<option value="{{$gData->gen_title}}">{{$gData->gen_title}}</option>
#endforeach
</select>
In edit page, I want to display it as well but how to display it as the genre I selected when create?
In my opinion, you should use $gData->id as value of each option. Because normally you store the id as foreign key and not something like the title.
But to your question itself: You can simply add an if statement that checks within the foreach loop if the current item is the selected one. In my example I assume you have a variable $movie which is the entry you want to edit.
<select name="mov_genre" id="mov_genre" class="form-control input-sm">
<option value="">-- SELECT GENRE --</option>
#foreach(App\Genre::pluck('gen_title', 'id') as $key => $value)
<option value="{{ $key }}" {{ optional($movie)->genre_id === $key ? 'selected' : '' }}>{{ $value }}</option>
#endforeach
</select>
Please note that I swapped App\Genre::all() for App\Genre::pluck('gen_title', 'id'). This will only load the both fields gen_title and id and put them in an array where id is the key and gen_title the value. It is done for better performance as we do not load unnecessary data.
Also note that optional($movie) will return null when $movie is not set. This is done to reduce weird if statements and is common practice.
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 am attempting to create a drop down selection box for various attributes of a product which the user would select on a product page, such as weight or flavour or colour. My database for the products is currently as follows:
products(id,productname,productprice)
productattributes(id,productid,attributename,value)
I'm using Laravel. So far in my controller I have:
public function show($id)
{
$product = DB::table('products')->where('slug', $id)->take(1)->get();
$prodid = DB::table('products')->where('slug', $id)->pluck('id');
$prodattr = DB::table('productattributes')->where('prodid', $prodid)->get();
return View::make('product/singleproduct', ['product' => $product, 'prodattr' => $prodattr]);
}
And in the view I have:
#foreach ($prodattr as $prodattribute)
{{ $attrname = $prodattribute->attributename }}
{{ $attrvalue = $prodattribute->value }}
#endforeach
This gives me the output:
Flavour Unflavoured
Flavour Strawberry
Weight 1kg
So I have the data I need, but my problem is how to get this into a select box for each attribute. I won't always know what the attribute names will be (colour may be added, for example) or how many there will be, so I don't want to hard code 'Flavour' etc in. I essentially want to generate HTML like this:
<select name="flavour">
<option value="Unflavoured">Unflavoured</option>
<option value="Strawberry">Strawberry</option>
</select>
<br>
<select name="weight">
<option value="1kg">1kg</option>
</select>
Does anyone have any ideas? I'm thinking a loop within the loop, or maybe my table layout is less than ideal? I thought about adding another query within the foreach loop, but this seems counterproductive as I think I have all the data I need. I also wondered if it would be possible to combine or merge the arrays somehow? Any help is much appreciated!
You could create a third table in order to seperate custom attributes and available values for each attributes.
Example of structure I would use:
products(id, name, price)
product_meta_name(id, name)
product_meta_value(id, product_meta_name_id, product_id, value)
I'm relatively new to Laravel (and using Laravel 4), but been around PHP and C# a long time. Seems like this should be easy, but I can't find anywhere that tells me how to do this.
In my Controller I get the data from the database and send it to the view like this:
$sections = DB::table('paperSections')->lists('section','id');
return View::make('layouts.publisher.step2', array('sections' => $sections));
in my View, I have the following:
{{ Form::select('sections[]', $sections, '', array('multiple')) }}
which generates a select list like this:
<select multiple="multiple" id="sections" name="sections">
<option value="1">News</option>
<option value="2">Sports</option>
<option value="3">Features</option>
<option value="4">Arts and Entertainment</option>
<option value="5">Technology and Science</option>
<option value="6">Op-Ed</option>
</select>
Lets assume I have a string (e.g. "1,3,5") which represents the multiple options selected previously. How can I re-select those three options using that string?
Pass array of selected options as 3rd param:
$selected = explode(',', $idsAsString);
Form::select('sections[]', $sections, $selected, ['multiple'])
I'm currently working on a project with a search function. I'm getting some data from my database. I bind this data through my controller. And fill my dropdownlist with it, including a default select item. Is it possible to set the values from the select to the data I retrieve from my database?
// Browser
<select id="game_kind" name="game_kind">
<option value="" selected="selected">Make your choice..</option>
<option value="1">puzzle</option>
<option value="4">dice game</option>
<option value="5">card game</option>
<option value="6">board game</option>
</select>
// Controller
public function Filter()
{
$game_kinds = GameKind::where('game_kind_name_en','!=','')->lists('game_kind_name_en');
return View::make('web.spelzoeken', compact('game_kinds');
}
// View
{{ Form::open(['route' => 'web.search.post']), PHP_EOL }}
{{ Form::label('game_kind',Lang::get('web-zoeken.search-game'))}}
{{ Form::select('game_kind', ['' => Lang::get('web-zoeken.search-option')] + $game_kinds)}}
{{ Form::submit(Lang::get('web-zoeken.search-button'), ['class' => 'button tiny']), PHP_EOL }}
{{ Form::close(), PHP_EOL }}
// Result I really want
<select id="game_kind" name="game_kind">
<option value="" selected="selected">Make your choice..</option>
<option value="puzzle">puzzle</option>
<option value="dice game">dice game</option>
<option value="card game">card game</option>
<option value="board game">board game</option>
</select>
Thanks in advance!
Change this:
$game_kinds = GameKind::where('game_kind_name_en','!=','')
->lists('game_kind_name_en');
into this:
// SELECT 'field' AS 'alias' ...
$game_kinds = GameKind::select('game_kind_name_en', 'game_kind_name_en as game_kind_name_en_key')
->where('game_kind_name_en','!=','')
->lists('game_kind_name_en', 'game_kind_name_en_key');
From the docs:
Specifying A Select Clause
$users = DB::table('users')->select('name as user_name')->get();
or ...->lists(...) in your case
And
Retrieving A List Of Column Values
$roles = DB::table('roles')->lists('title');
This method will return
an array of role titles. You may also specify a custom key column for
the returned array:
$roles = DB::table('roles')->lists('title', 'name');
BTW
- Don't use spaces in HTML-attribute names.
Instead of doing:
value="dice game"
Do like:
value="dice_game".