how to display both id and value in dropdown list in laravel? - laravel

This is my code in my View
{{ Form::label('supplier_list', 'Supplier', array('class' => 'control-label')) }}
{{ Form::select('supplier', $supplier_list, null, array('class' => 'form-control')) }}
This is my code in controller
$supplier_list = Supplier::lists('supplier_name', 'id');
My ouput is a Dropdown of SupplierName, what would I need is a Dropdown of displaying both SupplierName and ID, How to do it in laravel 4.2 ?

Try this:
In Controller
$supplier_list = Supplier::lists('supplier_name', 'id')->get();
In Template
<select name="id" class="form-control">
<option value="">Select Supplier Name</option>
#foreach ($supplier_list as $key => $v)
<option value="{!! $v['id'] !!}">{!! $v['supplier_name'] !!}{!! $v['id'] !!}</option>
#endforeach
</select>

Related

Laravel not validate html select box

I have problem with validating html select. I put rules and all other form controls work fine but only select not show validate message.
Model
class Post extends Model
{
protected $table = 'post';
static $rules = [
'title' => 'required',
'slug' => 'required',
'user_id' => 'required|not_in:0',
'meta_title' => 'required',
'meta_description' => 'required',
'post_category_id' => 'required'
];
}
Blade
<div class="form-group">
{{ Form::label('post_category_id', 'Category') }}
<select name="post_category_id" class="form-control">
<option value="0">Select Category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == $post->post_category_id ? 'selected' : ''}}>{{ $category->name }}</option>
#endforeach
</select> {!! $errors->first('post_category_id', '<div class="invalid-feedback">:message</div>') !!}
</div>
Controller
public function store(Request $request)
{
request()->validate(Post::$rules);
$post = Post::create($request->all());
Alert::alert('Success', 'Post created successfully.', 'Type');
return redirect()->route('admin.post.index');
}
What i do wrong here?
Even if the option has the value 0 it is still present so, no error will be triggered for "required" rule
Use a disabled option instead
<select name="post_category_id" class="form-control">
<option disabled>Select a Category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == $post->post_category_id ? 'selected' : ''}}>{{ $category->name }}</option>
#endforeach
</select>
As far as I see, you have a default value set on your placeholder of the select, so the input will always be valid since its always sent to the backend.
take a look at this line : Select Category
you're setting a default value, remove it and try again:
in your blade do this:
<div class="form-group">
{{ Form::label('post_category_id', 'Category') }}
<select name="post_category_id" class="form-control">
<option>Select Category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == $post->post_category_id ? 'selected' : ''}}>{{ $category->name }}</option>
#endforeach
</select> {!! $errors->first('post_category_id', '<div class="invalid-feedback">:message</div>') !!}
</div>

How get old value on select in laravel blade?

i have filter form using select dropdown , but i cant get this old value after i submit this filter like input like type text . someone can help ?
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
i add my function controller :
public function search_filter_alkes(Request $request)
{
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$user_id = $request->user_id;
$alat = Alat::with('users')->where('user_id',$user_id)
->where('jenis', 'Alkes')->
get();
session()->put('user_id',$user_id);
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
->with('user_id', $user_id)
;
}
as per your code use this and make sure you use withInput()
return redirect()->back()->withErrors($validator)->withInput();
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}" {{ old('user_id') == $id ? "selected" :""}}>{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
EDITED
When you use redirect() after post method you have to use withInput() as i mention above example
return redirect('route')->withInput();
when you use view() you have to pass data like as array like your code then use same variable name
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
<option value="{{ $id }}" {{ $user_id == $id ? "selected" :""}}>{{ $nama_unit }}</option>
You wil have to compare old value with input key. So replace the below code inside your for loop
#if (Input::old('user_id') == $id)
<option value="{{ $id }}" selected>{{ $nama_unit }}</option>
#else
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endif
You have multiple options:
<select name="tags[]" class="form-control select-tag" multiple>
#foreach($tags as $tag)
<option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected": ""}}>{{$tag->name}}</option>
#endforeach
</select>
OR
<select name="gender" class="form-control" id="gender">
<option value="">Select Gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
</select>
This worked for me in laravel 8:
<select class="form-control" id="gender" name="gender" required>
<option value="">Select gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
<option value="O" #if (old('gender') == "O") {{ 'selected' }} #endif>Other</option>
</select>

Laravel chosen-select retrieving old value from the form

I am trying to retrieve old value from my form on the edit form, but it seems impossible to find an answer, because everyone in the world decided to use Form::select from laravelcollective/html library. I am trying to use the regular HTML to work this, and it's not able to retrieve old value from the form.
This is my HTML Code.
<div class="col-xs-12 form-group">
<label class="control-label">Role*</label>
<select name="roles[]" data-placeholder="Choose a Role for this User..." class="chosen-select" multiple tabindex="4">
<option value="">Select</option>
#foreach ($roles as $key => $value)
<option value="{{ $key }}" {{ in_array($key, old("roles")) ? "selected":"") }} >{{ $value }}</option>
#endforeach
</select>
</div>
This is my controller code.
public function edit($id)
{
$roles = Role::get()->pluck('name', 'id');
$user = User::findOrFail($id);
return view('admin.users.edit', compact('user', 'roles'));
}
I am trying to convert the following into regular html
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('role', trans('global.users.fields.role').'*', ['class' => 'control-label']) !!}
{!! Form::select('role[]', $roles, old('role') ? old('role') : $user->role->pluck('id')->toArray(), ['class' => 'form-control select2', 'multiple' => 'multiple', 'required' => '']) !!}
<p class="help-block"></p>
#if($errors->has('role'))
<p class="help-block">
{{ $errors->first('role') }}
</p>
#endif
</div>
</div>
Try this:
<div class="col-xs-12 form-group {{ $errors->has('role') ? 'has-error' : '' }}">
<label for="role" class="control-label">Role *</label>
<select class="form-control select2" name="role[]" id="role" required multiple>
#foreach ($roles as $id => $label)
<option value="{{ $id }}" #if (collect(old('role', $user->role->pluck('id') ?? []))->contains($id)) selected="selected" #endif>{{ $label }}</option>
#endforeach
</select>
<span class="help-block">
#if ($errors->has('role'))
{{ $errors->first('role') }}
#endif
</span>
</div>
In your controller's update/store function, make sure you either use the built-in validation methods, which will populate the session on any errors.
or
If you have other checks, make sure you return the input when redirecting back.
return redirect()->back()
->withInput() // <- return input with redirect, will populate session
->with('error', 'Persist failed'); // <- optional

Laravel select optgroup and separated values from database

I write form to send values to database:
{!! Form::model($product, ['method'=>'PATCH','class'=>'form-horizontal','action'=>['ProductsController#updatechangeStatus', $product->id]]) !!}
{{ Form::select('userstan_id', array(
'Użytkownicy' => array($userList),
'Magazyny' => array($warehouseList),
), null, ['class' => 'form-control'])}}
{!! Form::submit('Tak, przenieś', ['class' => 'btn btn-primary']) !!}
{!! link_to('warehouse/'. $product->id, $title = 'Anuluj', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
public function changestatus($id){
$product = Product::find($id);
$userList = User::lists('name', 'id');
$warehouseList = warehouse::lists('name_warehouse', 'id');
return view('pages.changestatus', compact('userList', 'product', 'warehouseList'));
}
Laravel returned values in Form::select like this: http://iv.pl/images/97320689105137896288.png
but I want to return a result like this:
http://iv.pl/images/48707724105931389272.png
How can I do to be returned only name user and name warehouse and value was id number in Laravel (I tring with foreach, but in array foreach not working), like this:
<select>
<optgroup label="Użytkownicy">
<option value="1">michal</option>
<option value="2">mateusz</option>
</optgroup>
<optgroup label="Magazyny">
<option value="1">kosz</option>
<option value="2">zaginione</option>
</optgroup>
</select>
OK I write this: (now Laravel returned records correct look: klik!)
<select name="userstan_id" class="form-control">
<optgroup label="Użytkownicy">
#foreach($userListName as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
#endforeach
</optgroup>
<optgroup label="Magazyny">
#foreach($warehouseList as $warehouse)
<option value="{{ $warehouse->id }}">{{ $warehouse->name_warehouse }}</option>
#endforeach
</optgroup>
</select>
Where name="userstan_id" is name of column where you want save or update record ;)

How to set class attribute on a per-option basis (in a select)?

I can create a <select> with something like this:
{{ Form::select('age', [
'young' => 'Under 18',
'adult' => '19 to 30',
'adult2' => 'Over 30']
) }}
Which will render out like this:
<select name="age">
<option value="young">Under 18</option>
<option value="adult">19 to 30</option>
<option value="adult2">Over 30</option>
</select>
But how can I add a class attribute to each of the <option>s?
If I have to write out the HTML, how can I maintain the pre-selected option based on the model data?
As suspected, you'll need to do this yourself. I usually pass the selected option from my controller:
// Controller
return view('my-view', ['ages' => $ages, 'selectedAge' = $selectedAge]);
// my-view.blade.php
<select name="age">
#foreach ($ages as $value => $name)
<?php $selected = ($selectedAge === $value) ? 'selected="selected"' : ''; ?>
<option class="age-{{ $value }}" value="{{ $value }}" {{ $selected }}>
{{ $name }}
</option>
#endforeach
</select>

Resources