Laravel: Send default value to path in web.php - 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')

Related

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 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.

Dropdown Error : Type error: Too few arguments to function App\Http\Controllers\EnfantsController::create(), 0 passed and exactly 1 expected

So, I have an assistant and enfant tables. I made a dropdown list to affect an assistant from DB while the registration of an enfant. I don't know whats wrong I keep getting this error "Type error: Too few arguments to function App\Http\Controllers\EnfantsController::create(), 0 passed and exactly 1 expected". So this is my Enfants Controller :
public function create($nom)
{
$assistants =Assistant::all();
return view('enfants.create' ,['ass'=>$assistants, 'nom'=>$nom]);
}
And this is my create.blade.php :
<select class="form-control select2" id="ass_name" name="ass_name" required>
#foreach($ass as $value)
<option value="{{ $value->nom }}">{{ $value->nom }}</option>
#endforeach
</select>
And, in the option values , I want to have the name of the assistant and then get the id of the selected name and add it to the foreign key "assistant_id" in my enfant table. I hope you get what i want to do, i'm new to Laravel. Any help will be appreciated.
The issue is in your EnfantsController#create method. As you can see its expecting an argument which is a query param that you mention in your routes file. You need to pass value for $nom parameter when you make call to the create method.

Laravel: Variable not being passed from controller to view

using Laravel 5.6.28 my route on web.php file looks like this:
Route::get('kitysoftware/besttrades', 'SectorsController#besttradesview');
My controller with bestrasdeview function:
class SectorsController extends Controller
{
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
foreach ($sectors as $sector) {
echo $sector->SectorName;
}
//passing variable sectors1 to my view (is = $sector)
return view('besttradesview', ['sectors1' => $sector]);
}}
My view Bestradesview.blade.php is:
<form method="GET">
<div class="selectsector">
<Select class="selectsector" name = "sectors">
<option value="{{ sectors1 }}"></option>
<!-- test#2: <option value="{{ $sectors1->sector[] }}"></option> -->
<!-- test#3: <option value="{{ $sectors1->sector }}"></option> -->
</form>
</select>
And i get this error: Use of undefined constant sectors1 - assumed
'sectors1' (this will throw an Error in a future version of PHP)
When testing #2 commented line instead i get another error: Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Cannot use [] for reading
When testing #3 commented line without [] i get another error: Use of undefined constant sectors1 - assumed 'sectors1' (this will throw an Error in a future version of PHP)
Probably it's pretty simple but it's driving me nuts because i can't see why the variable is not passing to the view.
I know it's reading my table because if i remove the last return view call line on my controller, it echoes out all the values from my SectorName column, but i want it on a select dropdown menu.
I have been reading the docs, forums and watching laracast videos without luck. Any insight or just pointing me out to where to learn the proper sintax solution will be appreciatted.
Thanks in advance
It looks like you're trying to populate the select options from the query. If that's the case, your loop is in the wrong place.
Take the loop out of the controller method, and pass the $sectors collection directly to the view.
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
return view('besttradesview', ['sectors1' => $sectors]);
}
Then loop in the view to output the options.
<select class="selectsector" name="sectors">
#foreach($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
</select>
I assumed you wanted some text in the option. If you use the sector name as the option text it will also be used as the option value by default. If you want to use something else for the value, it would be like this, for example:
<option value="{{ $sector->id }}">{{ $sector->SectorName }}</option>
Change,
return view('besttradesview', ['sectors1' => $sector]);
To,
return view('besttradesview', ['sectors1' => $sectors]);
Notice the s in $sectors ($sectors = DB::table('Sectors')->get();) and in the actual view, it should be $sectors1 not just sectors1.
You also need to loop through actual sector:
#foreach ($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
Also, I would definitely suggest creating a model for your Sectors table rather than using the DB:: facade. You should make use of the M in MVC.

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