I have table user and branch like this
id name branch_code
1 ABC ["011","012","013","014"]
2 DEF ["014"]
table branch
id branch_code branch_name
1 011 XXX
2 012 YYY
3 013 ZZZ
4 014 WWW
when create a new user, there is multi-select dropdown for BRANCH, so one user can select many branches.
i can save the multi-select dropdown it into table user in column branch_code. But i have problem to show the selected value in edit form
here is the multi-select dropdown for edit form
<select class="form-control" name="branch[]" id="users" multiple>
#foreach ($branch as $branch)
<option #if ($branch->branch_code==$user->branch_code)
selected
#endif value="{{$branch->branch_code}}">
{{$branch->branch_name}}
</option>
#endforeach
</select>
the edit controller
public function edit($id)
{
$branch = Branch::where('status','1')->get();
$user = User::where('id',$id)->first();
return view('admin.edit', compact('user','branch'));
}
in edit form, i want to show all branches with selected branches and also unselected branches in the dropdown. and for selected branches, it will highlighted with "selected" remark or something like that. But now it's only show all branches and no remark for selected branches
$user->branch_code is array or json string of an array.
If it's json string then $user->branch_code = json_decode($user->branch_code) first.
When you have array - you can do something like:
<option
#if (in_array($branch->branch_code, $user->branch_code)) selected #endif
value="{{$branch->branch_code}}"
>
{{$branch->branch_name}}
</option>
Related
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.
In a laravel controller I currently have these two variables which are pulling from a their respective models.
$guests = Person::where('id', $id)
->with('languages')
->get();
$languages = Language::pluck('name')->toArray();
return view('layouts.visitor', ['guests' => $guests, 'languages' => $languages]);
in the database table I have a few different fields:
id | name
----------
1 | spanish
2 | english
3 | french
When selecting a language I'm able to select a language assigned to a user. I am also able to select all the languages in the table. What I would like to do and I'm not able to figure out is to have it show the current language assigned to a user first when he page loads then after if the user would like to change it they should view all the OTHER languages available in the drop down.
If the default language for this user was spanish it should be like this:
Drop down:
Spanish
English
French
You can add the selected attribute to the dropdown option if that is the user's chosen language:
<select name="language">
#foreach($languages as $language)
<option value="{{$language->id}}" #if($user->language_id == $language->id) selected #endif>{{$language->name}}</option>
#endforeach
</select>
I assume you are using one to many relationship. And your persons table has a language_id column which is foreign key of languages table.
In that case your controller should look like this:
public function show($id){
$guests = Person::query()->findOrFail($id); //find a row with an id
$languages = Language::pluck('name','id'); //'name' is the value and 'id' the key of the array
return view('layouts.visitor', ['guests' => $guests, 'languages' => $languages]);
}
Blade:
In blade keep the default value null. It will retrieve the value of language_id column from database.
{{ Form::select('language_id',$languages,null,['otherAttribute'=>'ifAny']) }}
The third parameter of the select method is responsible for selecting the current related property. So like smartrahat answer, you will correct your pluck to be ..pluck('name','id') then in view:
{{ Form::select('language_id',$languages,$guests->language,['class' => 'form-control']) }}
This solution assumes that there a property for Person model named language and it should be the language id integer.
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)
Within my update profile form, I have a select list for title. Admittedly, the items should be derived from the database however for the moment, I would like this particular select list to do the following..
Show the selected item on initial page load. So, if the $user['title'] is a 'Mrs', this is displayed.
Should laravel validation bring up errors on other form fields, and the user has changed the title to 'Mr', the selected state should be on the item 'Mr'.
I have the following so far, but it isn't working correctly.
<select name="title" id="title">
<option value="Mr" #if(($user['title']) == 'Mr') selected #else #if((Input::old('title')) == 'Mr') selected #endif #endif>Mr</option>
<option value="Mrs" #if(($user['title']) == 'Mrs') selected #else #if((Input::old('title')) == 'Mrs') selected #endif #endif>Mrs</option>
....
</select>
You should pass the data for the select from your Controller and also uset the Form component to build the select in the View, for example:
In Controller method:
$users = User::lists('title', 'id');
return View::make('user')->with('users', $user);
In the View:
// By default, item with id 1 (the first item) will be selected
echo Form::select('title', $users, Input::old('title', 1), ['id'=>title]);
If you are using Blade then:
// By default, item with id 1 (the first item) will be selected
{{ Form::select('title', $users, Input::old('title', 1), ['id'=>title]) }}
Read the documentation properly. You are using Laravel with old school PHP coding.
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)