How to disable an option from select laravel blade template - laravel-4

I'm passing this array to a blade view:
$data['publish'] = [
null => '- Select Ad Type -',
1 => 'Featured Ad',
2 => 'Normal Ad'
];
Within the view I'm creating a select input like this:
{{ Form::select('publish', $publish, null, array('class'=>'span10 m-wrap')) }}
How can I disable one option, for example Featured Ad?

You can't using the Form::select helper because the options use the 'value' => 'name' format for each array item, so there's no way to specify the additional disabled attribute. You just have to build the select manually using a #foreach loop and add the disabled attribute using a condition:
<select name="publish" class="span10 m-wrap">
#foreach ($publish as $value => $name)
<option value="{{ $value }}"
#if ($name == 'Featured Ad')
disabled
#endif
>
{{ $name }}
</option>
#endforeach
</select>
As an alternative you could create your custom Form::macro that uses some other parameters that allows disabling individual options, but it seem too much of a hassle unless you need to use this in a lot of places.

Related

How do I create insert related field value from id [Laravel]

First of all sorry if my title question doesn't make sense, since I don't know how to word my problem and my English not that great. Okay, so
I have my controller
Buy::create([
'master_item_id' => $request->master_item_id,
'quantity_item' => $request->quantity_item,
'price' => $request->price
]);
I have my view
<select id="master_item_id" name="master_item_id"
class="form-control #error('master_item_id') is-invalid #enderror">
<option></option>
#foreach($item as $value)
<option {{ old('master_item_id') ? 'selected' : '' }} value="{{ $value->id }}">
{{ $value->name_item }} |
Rp {{ number_format($value->price, 0, ',', '.')}}
</option>
#endforeach
</select>
How do I create insert 'price' from one form select above? I'm guessing you use where() or maybe find()? I've been flipping through website searching and couldn't find anything. Thanks!
Using the model called MasterItem, this could work:
Buy::create([
'master_item_id' => $request->master_item_id,
'quantity_item' => $request->quantity_item,
'price' => MasterItem::find($request->master_item_id)->price
]);
This does assume that master_item_id in your request represents the id or primary key of the MasterItem model.
If not, and you cannot use find() then this would also work:
Buy::create([
'master_item_id' => $request->master_item_id,
'quantity_item' => $request->quantity_item,
'price' => MasterItem::firstWhere('whatever_the_column_is_called', $request->master_item_id)->price
]);

Select input with 2 empty option values

In my laravel 5.7/mysql 5 app I have boolean is_quiz field in my vote Table and in model I define:
protected $casts = [
'is_quiz' => 'boolean',
];
...
And array with possible values/keys for using of this fields
private static $voteIsQuizLabelValueArray = Array(1 => 'Is Quiz', 0 => 'Is Not Quiz');
in control I add empty value for empty selector:
$viewParamsArray['voteIsQuizValueArray'] = $this->SetArrayHeader(['' => ' -Select Is Quiz- '], Vote::getVoteIsQuizValueArray(false));
and this array has values:
$viewParamsArray['voteIsQuizValueArray']::Array
(
[] => -Select Is Quiz-
[1] => Is Quiz
[0] => Is Not Quiz
)
In my form this array as :
{{ Form::select('is_quiz', $voteIsQuizValueArray, isset($vote->is_quiz) ? $vote->is_quiz : '', [ "id"=>"is_quiz", "class"=>"form-control editable_field select_input " ] ) }}
and in rendered html-source I see 2 options selected :
<select id="is_quiz" class="form-control editable_field select_input valid" name="is_quiz" aria-invalid="false" aria-describedby="is_quiz-error"><option value="" selected="selected"> -Select Is Quiz- </option><option value="1">Is Quiz</option><option value="0" selected="">Is Not Quiz</option></select>
and validator.w3.org raised error here.
I see what is the reson of the syntax error, but I do not know is there is easy way to fix it ?
Thanks!
Form builder is deprecated since Laravel 5
Why are Form and HTML helpers deprecated in Laravel 5.x?
As one of options you can try use Laravel Collective
https://packagist.org/packages/laravelcollective/html
But IMO the best decision is to build select via blade:
<select id="is_quiz"
class="form-control editable_field select_input valid"
name="is_quiz"
aria-invalid="false"
aria-describedby="is_quiz-error"
>
#foreach ($voteIsQuizValueArray as $k => $v)
<option
value="{{ $k }}"
#if( $k === old('is_quiz', '') ) selected="selected" #endif
>{{ $v }}</option>
#endforeach
</select>

Laravel blade form select statement has index for value

Having searched and tried to resolve this I can't seem to work it out.
I have a simple static method in a class that returns a list of options. I am using this to populate a select element in a form. This works fine but I'm using the Laravel blade shortcut language and the value of each option is coming out as the index of the array, which is sent to the database when persisting the results of the form:
{!! Form::select('type', \App\Http\Utilities\Airporttype::all(), null, ['class' => 'form-control'] ) !!}
HTML produced:
<select class="form-control" id="type" name="type">
<option value="0">Commercial</option>
<option value="1" selected="selected">Military</option>
<option value="2">Galactic</option><option value="3">Private</option>
</select>
Static method called:
class Airporttype
{
protected static $types = [
"Commercial",
"Military",
"Galactic",
"Private",
];
public static function all()
{
return static::$types;
}
}
By using the 'null' option, it will give me the option = selected if the database matches what is already saved for that record.
I can achieve it buy doing the following, but wanted to use the blade shortcode style as it's clean (below is what I have tested elsewhere in te form and works):
<select id="country" name="country" class="form-control">
#foreach (\App\Http\Utilities\Country::all() as $country => $code)
<option value="{{ $country }}" #if ($country == $airport->country) selected = 'selected' #endif>{{ $country }}</option>
#endforeach
</select>
Array dump of Aiporttype::all();
array (size=4)
0 => string 'Commercial' (length=10)
1 => string 'Military' (length=8)
2 => string 'Galactic' (length=8)
3 => string 'Private' (length=7)
Thanks
If I've understood you correctly, you need something like this:
{!! Form::select('type', \App\Http\Utilities\Airporttype::all(), $airport->country, ['class' => 'form-control'] ) !!}
UPDATE:
If you have types in strings, you can try to change your array to:
protected static $types = [
'commercial => 'Commercial',
'military' => 'Military',
'galactic' => 'Galactic',
'private' => 'Private',
];
To get an array indexed by its values you can try something like this
$types = \App\Http\Utilities\Airporttype::all();
$types = array_combine($types, $types);
-- view --
{!! Form::select('type', $types, null, ['class' => 'form-control'] ) !!}
Working Fine for me...
In controller
View::share('types',\App\Http\Utilities\Airporttype::all());
In View
{!! Form::select('types',array(''=>'- Select types -')+$types,Input::get('types',null),array('class'=>'form-control')) !!}
You can use array_flip function in php:
{!! Form::select('type', array_flip(\App\Http\Utilities\Airporttype::all()), $airport->country, ['class' => 'form-control'] ) !!}
Simple elegant function!

Laravel RESTful edit screen with multiple tables

I have 3 tables for storing business details. The main table and a phone numbers table, as a business can have any number of phone numbers attached to it.
Table 1 - Business listing
Table 2 - Business phone numbers
Table 3 - Links table 1 to table 2 as shown here
1 id int(11) No None AUTO_INCREMENT
2 listings_id int(11) No None
3 listingsphone_id int(11) No None
4 created_at timestamp
5 updated_at timestamp
My controller extracts the data from the database and passes it to the view
public function edit($id)
{
// get the listing
$listing = DB::table('listings')
->where('listings.id', '=', $id)
->first();
$listingphone = DB::table('listings')
->leftjoin('listings_listingsphone_rel', 'listings.id', '=', 'listings_listingsphone_rel.listings_id')
->leftjoin('listingsphone', 'listings_listingsphone_rel.listingsphone_id', '=', 'listingsphone.id')
->where('listings.id', '=', $id)
->get();
// show the edit form and pass the listing
return View::make('admin.listings.edit')
->with('listing', $listing)
->with('listingphone', $listingphone);
}
Blade then needs to create multiple Input rows in the form for outputting to the screen, one for each row that exists.
{{ Form::model($listing, array('route' => array('admin.listings.update', $listing->id), 'method' => 'PUT')) }}
<div class="form-group">
{{ Form::label('listingname', 'Company Name') }}
{{ Form::text('listingname', null, array('class' => 'form-control')) }}
</div>
#foreach($listingphone as $key => $value)
<div class="form-group">
{{ Form::label('phone', 'Phone number') }}
{{ Form::text('phone', null, array('class' => 'form-control')) }}
</div>
#endforeach
{{ Form::submit('Edit the Listing!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
The problem with the code is, it will generate for me multiple Input lines with the same Name and ID
<label for="phone">Phone</label>
<input class="form-control" name="phone" type="text" id="phone">
<label for="phone">Phone</label>
<input class="form-control" name="phone" type="text" id="phone">
I obviously need to store that data in the controller with code such as this (psuedo code):-
// store
$listing = Listing::find($id);
$listing->listings_company = Input::get('listings_company');
$listing->save();
// store
$listingphone = ListingPhone::find($id);
foreach ($listingphone as $key => $value)
{
$listingphone->listings_phone = Input::get('phone['$key']');
$listingphone->save();
}
How should I be handling this type of setup within Blade/the controller?
I'm assuming that the problem you are having the text form fields is the same kind of issue that you need to deal with when you have a checkbox. You have multiple fields with the same name that needs to be referenced anyway.
For that you need to use brackets on the form field name.
You can see a similar example related to checkboxes here:
How to get the values for a series of checkboxes in Laravel 4 controller (if checked)

Laravel 4 blade drop-down list class attribute

Laravel blade drop down list class attribute not working.
I cannot find any reference to class or assigning attributes to select / drop-down lists in the documentation.
http://www.laravel.com/docs/html#drop-down-lists
Examples tried:
{{ Form::select('product_id', $productList, array('class'=>'form-control')) }}
{{ Form::select('product_id', $productList, $attributes = array('class'=>'form-control')) }}
Both return the same html but without the class attribute:
<select id="product_id" name="product_id">
... Option Stuff ...
</select>
{{ Form::select('product_id', $productList, null, array('class' => 'form-control')) }}
The third parameter is the key of the currently selected option. Defaults to null.
First get and create list in Controller for example:
$username_lists = Users::lists('username','id');
pass data to view by:
return View::make('layouts.customers')
->with('username_lists', $username_lists);
now get in view:
{{ Form::select('username_lists', $username_lists, null, array('class' => 'form-control')) }}

Resources