Get ID from Form:select inside if-else in Laravel - laravel

I have 2 Form::select's inside an id-else statement in my view in Laravel 5.5. How do I get the id from these? Tried to put the ID in there on several location but none works..
this is part of the view:
<div class="o-grid__col u-12/12#sm">
{!! Form::label('', __('profile.contactSalutation').'*') !!}
#if( Session::get('urlLang') == "en" )
{!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
#else
{!! Form::select(__('contact_contactSalutation'), array('Frau' => 'Frau', 'Herr' => 'Herr'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
#endif
</div>

This is the definition of Form::select:
{!! Form::select(NAME, OPTIONS, DEFAULT_SELECTED, ['id' => 'test', 'class' => 'any-class', ... so on with the extras in this array]) !!}
in your case will be:
{!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'), null, array('id' => 'YOUR ID HERE', 'class' => 'c-dropdown c-dropdown__simple u-mb-x6', 'required' => 'required')) !!}
notice the null (so nothing pre-selected) as the third argument.

Try this
$choices = ['Frau'=> 'Frau', 'Herr'=> 'Herr'];
{!! Form::select(__('contact_contactSalutation'), $choices, null, ['id' => 'my_id', 'class'=> 'c-dropdown c-dropdown__simple u-mb-x6', 'required']) !!}
or //if you want to select a default value
{!! Form::select(__('contact_contactSalutation'), $choices, $result->contact ?? null, ['id' => 'my_id', 'class'=> 'c-dropdown c-dropdown__simple u-mb-x6', 'required']) !!}
$result->contact is your DB column name
3rd argument is used for a selected value you can use coalesce Operator either from a saved value or null

Related

Unsupported operand types (View: C:\Users\10User\fyp2.2\resources\views\route\editRoute.blade.php)

Routes
Route::get('/editRoute/{id?}',['uses'=>'RouteController#edit' , 'as' =>'route.editRoute']);
Edit View blade
{!! Form::select('driver_id', ['$driver_id' => '---Select Driver---']+$drivers, null, ['class' => 'form-control']) !!}
Edit Controller
$routes = Route::find($id);
return view('route.editRoute', compact('routes'));
You can change that in one of two ways:
Add placeholder:
{!! Form::select('driver_id', $drivers, null, ['class' => 'form-control', 'placeholder'=>'---Select Driver---']) !!}
Change current select list:
{!! Form::select('driver_id', ['' => '---Select Driver---']+$drivers, null, ['class' => 'form-control']) !!}
Both option will work but first is my preferred :)

get the request values in Laravel

I wish to make search query by datepicker and select field.
How could I get the requests values from below view file to controller?
Where could I modify in the code? thanks.
index.blade.php
<div class="form-group col-sm-6">
{!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/pdfs/job_finished_search'))) !!}
{!! Form::input('text', 'datepicker_from', null, ['placeholder' => 'Fra', 'id' => 'datepicker_from']) !!}
{!! Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) !!}
{!! Form::select('customer_name', $jobs->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!}
{!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!}
{!! Form::close() !!}
</div>
Controller.php
public function job_finished_search(Request $request, Job $jobs)
{
$jobs = Job::onlyTrashed()
->whereBetween('created_at', array(
(Carbon::parse($request->input('datepicker_from'))->startOfDay()),
(Carbon::parse($request->input('datepicker_to'))->endOfDay())))
->where('customer_name', 'like', '%'.$request->customer_name.'%')
->orderBy('deleted_at', 'desc')
->paginate(15);
if (empty($jobs)){
Flash::error('Search result not found');
}
return view('pdfs.index', ['jobs' => $jobs]);
}
There are multiple way to get the request data e.g to get datepicker_from value you can use any of the below
$request->datepicker_from
$request->input('datepicker_from')
$request->get('datepicker_from')
choose the one you like the most
refer to https://laravel.com/docs/5.5/requests
To get request values you can use the get method, try:
$customer = $request->get('customer_name','default_value');
To get request values, you can set an object like $input= $request->all(). Then you can make use of the object which is an array to get at specific fields, e.g to access your date picker, you can write $input['datepicker_from']. You need to place $input= $request->all() before you declare the $jobs object in your code.

How to show multiple selected values of selectbox in Laravel 5.3?

I want to show multiple selected values in laravel5.3 form
{!! Form::select('category[]', $categories['all_cat']['categories'], null,['multiple' => 'multiple'], ['class' => 'form-control', 'id' => 'category']) !!}
Try with this:
{!! Form::select('category[]', $categories['all_cat']['categories'], null, ['multiple' => true, 'class' => 'form-control', 'id' => 'category']) !!}

Is there any way to prevent input type=“number” getting negative values?

How can I prevent the numbers which was given as input in the form, not to go as negative value?
Here is my code:
{!! Form::input('number', 'mobile', null, array('id' => 'mobile', 'class' => 'input-lg form-control TabOnEnter', 'placeholder' => 'Eg: 9876543210', 'tabindex' => 15)) !!}
Use the min attribute like this:
<input type="number" min="0">
In Laravel:
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => ....]) !!}
Use type="number" and min=0:
<input type="number" min="0">
Or:
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => ....]) !!}
You can also try Form::number:
{!! Form::number('number', 'mobile', ['min' =>0, 'id' => ....]) !!}
You can use server side validation provided by Laravel Validator class.
https://laravel.com/docs/5.3/validation#rule-min

Laravel 5 required_if validation for radio buttons

The laravel required_if validation doesn't seem to work when you have radio buttons.
I have the following rules:
'method' => 'required|in:Email,Url',
'email' => 'required_if:method,Email'|'email',
'url' => 'required_if:method,Url'|'url',
In my form I have the following:
{!! Form::radio('method', 'Email', true ) !!}
{!! Form::radio('method', 'Url', false ) !!}
{!! Form::text('email', null, ['maxlength' => '255', 'class' => 'form-control']) !!}
{!! Form::text('url', null, ['maxlength' => '1000', 'class' => 'form-control']) !!}
But the validation doesn't seem to fire?
My mistake - added extra apostrophes to the following:
'email' => 'required_if:method,Email'|'email',
'url' => 'required_if:method,Url'|'url',
Should be:
'email' => 'required_if:method,Email|email',
'url' => 'required_if:method,Url|url',

Resources