Laravel slective Relationship - php

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.

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

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

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)

Laravel: Send default value to path in web.php

Good afternoon.
I want that when starting my application, the index path leading to the dashboard receives a default value of 'month', so that the first thing the user sees is the information filtered by month. Already in the Dashboard I have a selector that will make the corresponding filter but I do not know if this affects the route already defined with the default value.
In web.php I have:
Route::get('merchant-dashboard/{filterDashboardMerchant}', 'HomeController#index')->name('merchant-dashboard')->middleware(['auth.shopify']);
$filterDashboardMerchant is the variable that I use to filter the information to display, it gets values ​​between 'today', 'month' and 'year'.
In the controller I receive it like this:
public function index($filterDashboardMerchant)
{
.............
}
The filter within the Dashboard view is the following select:
<select id="filter-records" class="custom-select-design1">
<option value="{{ route('merchant-dashboard', $filterDashboardMerchant = 'today') }}" {{$filterDashboardMerchant==='today' ? 'selected="selected"' : ''}}>Today</option>
<option value="{{ route('merchant-dashboard', $filterDashboardMerchant = 'month') }}" {{$filterDashboardMerchant==='month' ? 'selected="selected"' : ''}}>Month</option>
<option value="{{ route('merchant-dashboard', $filterDashboardMerchant = 'year') }}" {{$filterDashboardMerchant==='year' ? 'selected="selected"' : ''}}>Year</option>
</select>
Maybe it's a matter of syntax, but I've tried several options and none of them work for me. I hope you can help me.
Thank you.
One option is make parameter optional and in Controller set its default value.
Route:
Route::get('merchant-dashboard/{filterDashboardMerchant?}', 'HomeController#index')->name('merchant-dashboard')->middleware(['auth.shopify']);
Controller method:
public function index($filterDashboardMerchant = 'month')

Select drop down from table array

Here is part of my controller;
$agreements = Agreement::all();
print_r($agreements) //this works and displays an object/array with the details!
return View::make('individual_agreements.create')
->with('individual', $individual)
->with('agreements', $agreements)
->with('content_title', 'Create new individual agreement');
Here is part of my view;
<div class="form-group">
{{ Form::label('agreement_name', 'Agreement name') }}
{{ Form::select('agreement_name', $agreements->agreement_name) }}
</div>
I have a field called agreement_name in the table.. All I want to do is turn that in to a dropdown in the most efficient Laravel/Eloquent way as possible.
I keep getting the standard "agreement_name not defined" error and I cannot find a suitable example anywhere online having looked for over an hour now.
If you need the agreements for something else on the same page try
<div class="form-group">
{{ Form::label('agreement_name', 'Agreement name') }}
{{ Form::select('agreement_name', $agreements->lists('agreement_name', 'agreement_name')) }}
</div>
This will get all the agreements names within your agreements collection and allow them to be used within the select drop down. First parameter is the column and second is the key.
Else just change your query to;
$agreements = Agreement::lists('agreement_name', 'id');
You should do this
$agreements = Agreement::lists('agreement_name', 'id');
all return you all columns in table, so that's why your code wont work
The way you have this set up allows for the use of a foreach loop to list through and display results:
<select name="agreement_name" class="">
<option value="">- Select -</option>
#foreach($agreements AS $agreement)
<option value="{{ $agreement->id }}">{{ $agreement->agreement_name }}</option>
#endforeach
</select>
Since your $agreements is an array of elements, calling $agreements->agreement_name wouldn't work, while $agreements[0]->agreement_name probably would. If you want to use the Form::select() option, you'll need to use a different method of getting your agreements. Check the other answers for that solution.
Hope this provided some insight!
Edit
I should note that this isn't the Laravel Way of doing things, but it is certainly viable.

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