Pre-selecting value on edit page with blade in laravel? - php

I have an issue where i want to show the current brand in a select, on the edit page of a vehicle mode.
The select has to contain all the brands, so it is possible to choose another brand on editing the model.
I have this in my vehicleModelsController edit:
$vehicle_brands = VehicleBrand::all();
$selected_vehicle_brand = VehicleBrand::first()->vehicle_brand_id;
return view('vehicleModels.edit', compact(['vehicleModel', 'vehicle_brands'], ['selected_vehicle_brand']));
And in my edit.blade file i have the following select:
<select class="form-control" name="vehicle_brand_id">
#if ($vehicle_brands->count())
#foreach($vehicle_brands as $vehicle_brand)
<option value="{{ $vehicle_brand->id }}" {{ $selected_vehicle_brand == $vehicle_brand->id ? 'selected="selected"' : '' }}>
{{ $vehicle_brand->brand }}</option>
#endforeach
#endif
</select>
And it works just fine with editing, but it does not show the current value as selected, it shows the first value in the brand table.
I have Brand1, Brand2, Brand3 and Brand4 as test values in the brand table, but no matter what brand the model is related to, it shows Brand1 in the select on the edit page.
Any help is greatly appreciated!

In the controller you are assigning value of VehicleBrand::first()->vehicle_brand_id to $selected_vehicle_brand and then you are comparing it with id field in {{ $selected_vehicle_brand == $vehicle_brand->id ? 'selected="selected"' : '' }}
If there is not column/field named vehicle_brand_id on VehicleBrand model then VehicleBrand::first()->vehicle_brand_id will be null. So when you compare $selected_vehicle_brand == $vehicle_brand_id it will be like null == $vehicle_brand->id which will be false for any id value.
So it should either be (in Controller)
$selected_vehicle_brand = VehicleBrand::first()->id;
//Here we are hard coding the value to be the id of first record of VehicleBrand
//However in practice it should come from either request/route param eg: $id received via the request/route param
//Or from VehicleModel being edited eg: $vehicleModel->vehicle_brand->id
And {{ $selected_vehicle_brand == $vehicle_brand->id ? 'selected="selected"' : '' }} in the view
Or, if vehicle_brand_id column/field exists on VehicleBrand, it should be (in Controller)
$selected_vehicle_brand = VehicleBrand::first()->vehicle_brand_id;
and {{ $selected_vehicle_brand == $vehicle_brand->vehicle_brand_id ? 'selected="selected"' : '' }}
The field you are comparing the values for should be the same.
And as a side note, if you are on Laravel 9 you can use #selected blade directive
#selected(old($vehicle_brand->id) == $selected_vehicle_brand)

Related

Laravel PHP 8 blade -> getting a list of all parent table records, list them in a select element and selecting the record->name based on the foreignID

So to break that title down...
I have 2 tables -> bosses (parent table) & employees (child)
bosses hasMany employees and employees hasOne boss
What I need to accomplish is to list all the bosses in an element and "select" the current boss. And if I change the boss from the list, it should change the id of the element to the new selection.
#foreach ($bosses as $boss)
<option value="{{ $boss->id }}" {{ $boss->id == ($employee->boss_id) ? 'selected' : '' }}>
{{ $boss->full_name }}
</option>
#endforeach
Controller
public function details($id){
$employee = Employee::find($id);
$bosses = Boss::get();
return view('employees.details',[
'employee' => $employee,
'bosses' => $bosses,
] );
}
Right now I'm still setting everything up so the search isn't working.
With the above code I'm getting all of the $publishers listed by name but it is not selecting the the boss associated the the boss_id foreign key.
Thanks in advance!!!
You could use the helper method old() for this.
For example:
<select name="boss_id>
#foreach ($bosses as $boss)
<option value="{{ $boss->id }}" {{ (old('boss_id', $boss->id) == $employee->boss_id) ? 'selected' : '' }}>
More info: https://laravel.com/docs/9.x/validation#repopulating-forms

Laravel pivot table- getting only first value in the foreach loop

I am trying to loop through additional column data in pivot table. However, I am only getting the first value in the first row and rest are showing empty. Here is in my Blade
#foreach($benefits as $benefit)
#foreach($user->benefits as $benefitid)
value="{{ $benefitid->id === $benefit->id ? $benefitid->pivot->employee_pay : '' }}"
#endforeach
#endforeach
For the checkbox, similar condition is working, but not for the data. Here is the working code for checkbox
#foreach($benefits as $benefit)
#foreach($user->benefits as $benefitId)
{{ $benefitId->id === $benefit->id ? 'checked' : '' }}
#endforeach
#endforeach
I have a pivot table with following
benefit_id, user_id and employee_pay. The last one is the additional column. The relation is many to many.
I want to retrieve all the values in employee_pay column from the foreach loop. However, I am only getting the first value.

Laravel slective Relationship

I am just building my first Laravel App and have the following question:
Simplified Table Structure:
Table CORPORATE
id pk
name
checkbox "isSupplier"
checkbox "isManufacturer"
Table COMPONENT
id pk
name
if_corporate fk (where isManufacturer=true)
So my question is, where do I need to put hands on the code to get this "selector" implemented?
Thanks
edit:
resources/views/admin/components/create.blade.php
<select class="form-control select2 {{ $errors->has('corp') ? 'is-invalid' : '' }}" name="corp_id" id="corp_id" required>
#foreach($corps as $id => $corp)
<option value="{{ $id }}" {{ old('corp_id') == $id ? 'selected' : '' }}>{{ $corp }}</option>
#endforeach
</select>
is this the correct place to modify?
You are already too late in the code you have shared. That is the view, which comes after the controller in this instance.
The controller is probably called something like ComponentsController there will be a line of code that says
$corps = Corporates::all();
Or something like that. That is where you need to update it. Probably something like this:
$corps = Corporates::where(["isManufacturer" => true)->get();
Now $corps will be a collection of only ones that are manufacturers.

Using Laravel 4, how do I mark a radio button as checked if a session key is a specified value?

I have a multi-page form with two radio buttons with the same name attribute. When I select one and click the next step button, I save the value of that radio button into a session array with the form field name and chosen value. If the user comes back to the page, I want the previously chosen radiobutton to be checked.
This is what I came up with:
View: choose-listing-type.blade.php
<div class="form-group">
<?php $checked_status = Session::get('listing_form_data.type') === 'property' ? true : false; ?>
{{ Form::radio('type', 'property', $checked_status) }} Create Property Listing
</div>
<div class="form-group">
<?php $checked_status = Session::get('listing_form_data.type') === 'room' ? true : false; ?>
{{ Form::radio('type', 'room', $checked_status) }} Create Room Listing
</div>
This works, but it seems sloppy. First off, I don't think the if statement that checks the session value should be in the view, and I would love to find a way to do this in blade.
Using Laravel 4, what is the best practice to mark a radiobutton as checked depending on the value of a specified session key?
Since you mentioned you wanted to do it in the controller:
$type = Session::get('listing_form_data.type');
return View::make('view')->with('type', $type);
View:
{{ Form::radio('type', 'property', $type === 'property') }} Create Property Listing
{{ Form::radio('type', 'room', $type === 'room') }} Create Room Listing
Or even:
$type = Session::get('listing_form_data.type');
$isProperty = ($type === 'property');
$isRoom = ($type === 'room');
return View::make('view')->with(compact('isProperty', 'isRoom'));
View:
{{ Form::radio('type', 'property', $isProperty) }} Create Property Listing
{{ Form::radio('type', 'room', $isRoom) }} Create Room Listing
Why don't you just put the conditional right inline with the Form helper, like this:
<div class="form-group">
{{ Form::radio('type', 'room', (Session::get('listing_form_data.type') === 'room') ? true : false) }} Create Room Listing
</div>
Personally I don't see anything wrong with checking a session setting from the view...

form select on laravel

I have data look like this
id rel word
1 A word A
2 B word B
3 B word C
to get the data my controller look like this :
public function get_new()
{
return View::make('form.new')
->with('datas', Data::all());
}
and my View will look like this :
<select>
#foreach($datas as $data)
<option value="{{ $data->id }}" rel="{{ $data->rel }}">{{ $data->word }}</option>
#endforeach
</select>
How i can get that data with Form::select ?
If we use Form::select we can call that with Data::lists('word','id') and pass to the view.
but if i use this i just can get 2 data that is id and word.
how can i get all data that is id, rel and word with Form::select. Please help me.
Laravel 4s FormBuilder and HtmlBuilder aren't going to produce everything for everyone. Taylor himself has said that he wants to keep it lean and simple. The solution you have now is probably the best way you could go about it (using a simple loop).
Form::select isn't capable of automatically populating other attributes of the option element. If you want this functionality you're going to either have to make yourself a custom macro or write your own form generator.
Probably not the answer you're after but that pretty much sums it up.
Just want to update the solution...
<select name="name" id="name">
#foreach($datas as $data)
<option value="{{ $data->id }}" {{ (Input::old('name', 0) === $data->id ? ' selected="selected"' : '') }} rel="{{ $data->rel }}">{{ $data->word }}</option>
#endforeach
</select>
This will give old input on selected option

Categories