Laravel - Select defaulting to using Optgroup - select

I want to use a dropdown list in my view in laravel. In my controller I have the following.
$awards = array(
'gold' => 'gold',
'silver' => 'silver',
'bronze' => 'bronze',
'no-award' => 'No Award'
);
$entry->awards = $awards;
I pass it to the view as follows
$this->layout = View::make('entries/edit', array('entry' => $entry));
Finally in the view
<div class="form-group">
{{ Form::label('awards', 'Award:', array('class' => 'control-label '))}}
{{ Form::select('awards', array(entry->$awards))}}
</div>
The issue Im running into is in the source it looks like this...
<div class="form-group">
<label for="awards" class="control-label ">Award:</label>
<select id="awards" name="awards">
<optgroup label="0">
<option value="gold">gold</option>
<option value="silver">silver</option>
<option value="bronze">bronze</option>
<option value="no-award">No Award</option>
</optgroup>
</select>
</div>
Which looks good except I don't know how to avoid having it put in an optgroup, and I'd like to keep the array to my controller. Thanks.

You are wrapping your awards inside an extra unnecessary array - creating the optgroup[0]
Change
{{ Form::select('awards', array(entry->$awards)) }}
to
{{ Form::select('awards', entry->$awards) }}

Related

Laravel 5.8: Two round passing data between controller and blade

I am getting undefined variable when passing data from controller to blade the second time.
With Laravel 5.8, I have two actions/methods in a MyController. I have to pass data from controller to view. The first action works fine (MyController#action1 -> Blade1) but the second fails (MyController#action2 -> Blade2).
MyController:
public function action1()
{
...
$varialbe1 = ... // everything set correctly here and got it in blade1
return view('blade1', compact('variable1'));
}
public function action2(Request $request)
{
...
$association = $request->input('association');
return view('blade2', compact('association'));
}
Blade 1
<form method="POST" action="{{ route('route2') }}">
#csrf
<div class="form-group row">
<label for="association" class="col-md-4 col-form-label text-md-right">{{ __('Association') }}</label>
<div class="col-md-6">
<select required="required" id="association" class="form-control" name="association">
<option></option>
#foreach ($variable1 as $key => $val)
<option value="{{ $key }}">{{ $val->id }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Submit') }}
</button>
</div>
</div>
</form>
Route
Route::post('/route2', 'MyController#action2')->name('route2');
Blade 2 (Undefined variable: association)
{{ $association }}
I have tried different ways to get data in MyController and different to pass data to Blade2 (the way that also works fine with the action1), including:
$association = Input::get('association');
return redirect()->to('/route2')->with('association', $association); // where route2 load the view
My route file seems good (MyController in the previous post = LoginController). Here is my route/web.php:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::post('/route2', 'Auth\LoginController#action2')->name('route2');
Got resolved from another forum.
Should add the action method to the list of exception methods in the middleware as follow:
$this->middleware('guest')->except(['logout', 'action']);
As controller has the below middleware in the constructor:
$this->middleware('guest')->except('logout');
If the User is already logged in, which is the case, they will be redirected to the /home and the controller method will never be reached.

Vue multiple select

I would like to have multiple selections without multiple.vue librabry. I want to write it by my own. I have data like this in js:
data:
roles : [here data]
form.roles: []
And my html with Vue:
<select class="form-control required-select2" v-model="form.roles" id="roles" required>
<option v-for="role in roles" v-bind:value="role.id" v-bind:selected="form.roles.indexOf(role.id) > -1">
{{ role.display_name }}
</option>
</select>
How can I have in form.roles more than one role.id? I have to write maybe a method for this?
Do you want to do something like https://codepen.io/ittus/pen/vjdLvb
You need to add multiselect options to your select
<template>
<select class="form-control required-select2" v-model="form.roles" id="roles" multiple required>
<option v-for="role in roles" v-bind:value="role.id" v-bind:selected="form.roles.indexOf(role.id) > -1">
{{ role.display_name }}
</option>
</select>
<div>Selected: {{ form.roles }}</div>

Laravel Select Name and id of Model and use in SelectBox

This is my current Function in my Controller:
public function addProduct()
{
$categories = Category::all();
return view('admin.products.add')->with('categories', $categories);
}
I want to only select the columns "name" and "id" from the Category Model and assign them to the selectbox in my view like this:
<select>
<option value="ID">NAME</option>
</select>
How can I do this using the Form-Facade?
try with pluck method from the laravel Collection:
$categories = Category::all()->pluck('name', 'id');
and then in the view:
{!! Form::select('name', $categories) !!}
Try this generate all category
<select name="category" class="form-control">
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
And if you want to set particular category selected then do this
<select name="category" class="form-control">
#foreach($categories as $category)
<option value="{{ $category->id }}" <?php if($hotel->category_id == $category->id) { echo "selected";}?>>{{ $category->name }}</option>
#endforeach
</select>
You can use foreach here
In your blade view.
<select name = "category[]">
#foreach($categories as $category)
<option value = "{{$category->id}}">{{$category->youValueHere}}></option>
#endforeach
</select>
Lists was deprecated in L5.2 and removed on L5.3
Try this, simple & neat.
$categories = Category::lists('name', 'id');
{!! Form::select('categories[]', $categories, null, [
'class' => 'form-control', 'multiple'
]) !!}

Laravel 4 Preview Form Submit

Any idea how to solve the following approach.
I have a form and want to display the entered data in a specific formtemplate before store it in the DB. If the entered data looks properly, the user can save the form.So I am searching for a way to display the entered data as a preview in a new window/tab first. With my code below I am not able to preview the form without saving the data in the databse. Also display the preview in a new window or tab is not possible. I guess there is no way to achieve this with php / laravel. I tried some onlick events, but no luck. As it seems like the route is prefered.
Any idea how to solve this?
My form looks like:
{{ Form::open(array('url' => 'backend/menubuilder/'.$id, 'method' => 'PUT'))}}
<section>
<div class="container">
<div class="row">
<div class="inputBox">
<div class="col-xs-12 col-md-6">
<h3>Montag</h3>
<div class="form-group">
{{Form::label('gericht_1_mo','Gericht 1')}}
{{Form::textarea('gericht_1_mo', Auth::user()->gericht_1_mo,array('class' => 'form-control'))}}
</div>
<div class="form-group">
{{Form::label('preis_1_mo','Preis', array('class' => 'col-md-6'))}}
{{Form::text('preis_1_mo', Auth::user()->preis_1_mo, array('class' => 'col-md-6'))}}
</div>
<div class="form-group mrgT55">
{{Form::label('gericht_2_mo','Gericht 2')}}
{{Form::textarea('gericht_2_mo', Auth::user()->gericht_2_mo,array('class' => 'form-control'))}}
</div>
<div class="form-group">
{{Form::label('preis_2_mo','Preis', array('class' => 'col-md-6'))}}
{{Form::text('preis_2_mo', Auth::user()->preis_2_mo, array('class' => 'col-md-6'))}}
</div>
</div>
</div>
</div>
</div>
</div>
{{Form::submit('update')}}
{{-- <input type="submit" name="preview" value="preview"> --}}
{{Form::close()}}
{{ Form::open(array('url' => 'backend/menubuilder/templatesview/'.$id, 'method' => 'POST'))}}
{{-- {{Form::submit('Preview',array('onClick' => 'target_blank'))}} --}}
<input onclick="newTab()" type="submit" name="preview" value="preview" >
{{Form::close()}}
My routes:
Route::get('backend/menubuilder/templates/{id}', 'MenuBuilderController#template');
Route::post('backend/menubuilder/templatesview/{id}', 'MenuBuilderController#preview');
My Controller:
public function preview($id)
{
$user = User::find($id);
$owner = (Auth::id() === (int) $id);
return View::make('backend/menubuilder/templatesview/tempone')->withUser($user)->withOwner($owner);
}
public function template($id)
{
$user = User::find($id);
$owner = (Auth::id() === (int) $id);
return View::make('backend/menubuilder/templates/tempone')->withUser($user)->withOwner($owner);
}

Laravel 5.2 - assign class to option in select box

how one can assign class="some_class" to option in select box?
i would like to have
<select class="my_class" name="example">
<option selected="selected" value="">Choose something ...</option>
<option value="1" class="some_class">Hello</option>
<option value="2" class="some_class">World</option>
</select>
But I don't have any idea how to do it.. my form looks like:
{{ Form::select('example', array('1' => 'Hello', '2' => 'World'), null, array('class' => 'my_class', 'placeholder' => 'Choose position ...')) }}
How can I add "some_class" attribute to OPTION menu ?
The Form builder doesn't support class attributes for option tags. You will either need to build the select manually, or use JavaScript / jQuery to add the class to the option tags.
You can use my_class similar to this:
.my_class select option{
font-size: 20pt;
color:red;
}
This will affect only <option> elements of your <select> element.
Also, you can use JS to do this.