So I'm very new to laravel and I have a filter in my website blade as follows:
goal: the user chooses a city and price from and price to and then clicks search and villas with what selected must be shown on another view! all values from the database!
I've tried to do this:
the route:
Route::get('/searched/{city_id}/{pricefrom}/{priceto}', [WebsiteController::class, 'search'])->name('search.clicked');
the controller:
public function index() {
$cities = DB::table('cities')
->select('*')->distinct()->get();
$users = Villa::with('City','Seller', 'Payment')->get();
$pricefrom = DB::table('villas')
->select('price')
->where('price', '<=', 50000)->distinct()->get();
$priceto = DB::table('villas')
->select('price')
->where('price', '>', 50000)->distinct()->get();
return view('website', compact('users','cities','pricefrom','priceto'));
}
public function search($city_id, $pricefrom, $priceto) {
$city = City::find($city_id);
$price = Villa::find($price);
$citydata = Villa::with('City','Seller', 'Payment')->where('city_id', $city_id)->get();
$lowdata = Villa::with('City','Seller', 'Payment')->where('price', $pricefrom)->get();
$highdata = Villa::with('City','Seller', 'Payment')->where('price', $priceto)->get();
$info = Villa::with('City','Seller', 'Payment')->get();
return view ('searched',compact('citydata','lowdata','highdata','info'))->with('city', $city )->with('price', $price);
}
the website view blade: here I didn't know how exactly I should pass the parameters for the route so I get an error
#foreach ($users as $villa)
<form action="{{ route('search.clicked', $villa->city_id,$villa->pricefrom,$villa->priceto) }}" method="get">
#csrf
<select name="city" class="form-select">
<option value="" id="searchoption"> City </option>
#foreach ($cities as $city)
<option >{{ $city->name }}</option>
#endforeach
</select>
<div class="col-lg-3 p-2">
<select name="pricefrom" class="form-select">
<option value="" id="searchoption"> Price From </option>
#foreach ($pricefrom as $low)
<option >{{ $low->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<select name="priceto" class="form-select">
<option value="" id="searchoption"> Price To </option>
#foreach ($priceto as $high)
<option >{{ $high->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<button type="button" class="btn btn-outline-success">search</button>
</div>
</form>
#endforeach
also Villa model
class Villa extends Model {
use HasFactory;
protected $fillable=[
"id", "title", "description", "price", "state", "status", "city_id", "seller_id", "payment_id",
"created_at", "updated_at"
];
public function City()
{
return $this -> hasOne(City::class,'id','city_id');
}
public function Seller()
{
return $this -> hasOne(Seller::class,'id','seller_id');
}
public function Payment()
{
return $this -> hasOne(Payment::class,'id','payment_id');
}
}
I think I have many errors I should fix And I want some help with my search bar!
I get this error:
Missing required parameters for [Route: search.clicked] [URI: searched/{city_id}/{pricefrom}/{priceto}] [Missing parameters: pricefrom, priceto].
try:
<form action="{{ route('search.clicked', [$villa->city_id,$villa->pricefrom,$villa->priceto]) }}" method="get">
According to route function second parameter should be string or array. String or Array if you're passing only 1 & array if you are passing 2 or more.
function route($name, $parameters = [], $absolute = true)
BLADE FILE:
#foreach ($users as $villa)
<form action="{{ route('search.clicked', $villa->city_id,$villa->pricefrom,$villa->priceto) }}" method="get">
#csrf
<select name="city" class="form-select">
<option value="" id="searchoption"> City </option>
#foreach ($cities as $city)
<option >{{ $city->name }}</option>
#endforeach
</select>
<div class="col-lg-3 p-2">
<select name="pricefrom" class="form-select">
<option value="" id="searchoption"> Price From </option>
#foreach ($pricefrom as $low)
<option >{{ $low->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<select name="priceto" class="form-select">
<option value="" id="searchoption"> Price To </option>
#foreach ($priceto as $high)
<option >{{ $high->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<button type="button" class="btn btn-outline-success">search</button>
</div>
</form>
#endforeach
to
#foreach ($users as $villa)
<form action="{{ route('search.clicked', ['city_id' => $villa->city_id, 'pricfrom' => $villa->pricefrom, 'priceto' =>$villa->priceto]) }}" method="get">
#csrf
<select name="city" class="form-select">
<option value="" id="searchoption"> City </option>
#foreach ($villa->cities as $city)
<option >{{ $city->name }}</option>
#endforeach
</select>
<div class="col-lg-3 p-2">
<select name="pricefrom" class="form-select">
<option value="" id="searchoption"> Price From </option>
#foreach ($villa->pricefrom as $low)
<option >{{ $low->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<select name="priceto" class="form-select">
<option value="" id="searchoption"> Price To </option>
#foreach ($villa->priceto as $high)
<option >{{ $high->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<button type="button" class="btn btn-outline-success">search</button>
</div>
</form>
#endforeach
You need add to route :
<form action="{{ route('search.clicked',$object->first()->city_id,$model->first()->pricefrom,$model->first()->priceto) }}" method="get">
Controller:
public function index(){
$bike1 = Bike::get()->first();
$bike2 = Bike::get()->skip(1)->first();
return view('compare.index')->with(['bike1'=> $bike1, 'bike2'=> $bike2 ]);
}
Here I have passed only the first and second row to the blade view.
Blade view:
Select first bike
<form action ="{{ route('compare.get', [$bike1->id, $bike2->id]) }}" method="get">
<div class="first_bike" style="width:200px;">
<select name="bike1">
<option value="{{ $bike1->id }}"> {{ $bike1->name }}</option>
</select>
</div>
<br>
Select second bike
<div class="second_bike" style="width:200px;">
<select name="bike2">
<option value="{{ $bike2->id }}"> {{ $bike2->name }}</option>
</select>
</div>
<br>
<button type="submit"> Compare </button>
</form>
The get controller passes the value to another view that displays the data. It works.
How can I pass all the rows to the blade view? I know I can use all functions and loop the variable in the blade to get all the rows. Like this
#foreach($bike1 as $b1)
<option value="{{ $b1->id }}"> {{ $b1->name }}</option>
#endforeach
#foreach($bike2 as $b2)
<option value="{{ $b2->id }}"> {{ $b2->name }}</option>
#endforeach
If I do this, then how can I use the $b1 and $b2 as route parameters in the action properties in the form?
Can I manipulate the solution from the controller leaving my blade as it is? Or how do I change the blade view in order to achieve my goal? I am stuck
well if I am getting you correctly your Controller should be like
public function index(){
$bike1 = Bike::all();
$bike2 = Bike::all();
return view('compare.index')->with(['bike1'=> $bike1, 'bike2'=> $bike2 ]);
}
Then you modify your view to
Select first bike
<form action ="{{ route('compare.get'}}" method="get">
<div class="first_bike" style="width:200px;">
<select name="bike1">
#foreach($bike1 as $b1)
<option value="{{ $b1->id }}"> {{ $b1->name }}</option>
#endforeach
</select>
</div>
<br>
Select second bike
<div class="second_bike" style="width:200px;">
<select name="bike2">
#foreach($bike2 as $b2)
<option value="{{ $b2->id }}"> {{ $b2->name }}</option>
#endforeach
</select>
</div>
<br>
<button type="submit"> Compare </button>
</form>
For submitting you don't have to put them as route parameters,, just let the form submit and get the GET variables in the Controller that you are submitting the form to,,You can use the Request object to do this
$bike1 = Request->input('bike1');
$bike2 = Request->input('bike2');
hope you understand this,, just make sure the Request object is set as a parameter in the controller that you are submitting to
I am trying to create a multiple filter function to filter my table. I have a model called Product and it has collections named categories, colors and sizes. I have the following in my view:
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Categories') }}</label>
<select class="form-control" id="select-categories">
<option value="0">All</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Colors') }}</label>
<select class="form-control" id="select-colors">
<option value="0">All</option>
#foreach($colors as $color)
<option value="{{ $color->id }}">{{ $color->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Sizes') }}</label>
<select class="form-control" id="select-sizes">
<option value="0">All</option>
#foreach($sizes as $size)
<option value="{{ $size->id }}">{{ $size->name }}</option>
#endforeach
</select>
</div>
</div>
And then I use JQuery to send a AJAX request to my controller with the current selected option values of all three dropdowns.
Example data:
Categories: All
Colors: Red
Sizes: Small
Now I wish to show the products with the color red and size small but the category doesn't matter.
I get the collections with the use of one to many relationships and link tables.
$product->categories
$product->colors
$product->sizes
I am not sure how to approach this.
You can do something similar to below:
public function index(ProductRequest $request)
{
$query = Product::where($request->validated());
return ProductResource::collection($query);
}
and in your ProductRequest you can define filters that you want to allow
class ProductRequest extends FormRequest
{
...
public function rules()
{
return [
'categories' => 'exists:categories,id'
....
];
}
I am creating a website using laravel and I have a form to create a new users and within it i need an option to display a way to select a users role, i would like to do this in the form of a drop down list but I'm having difficulty figuring it out.. so far I have this (see below my create.blade.php), but it is not displaying the data from the three roles i have in the table in the DB, (these are admin,instructor and student).
<div class="form-group">
<label for="role">Role</label>
<select name="roles[]" class="form-control">
#foreach($roles as $role)
<ul class="dropdown-menu" role="menu">
{{ $role }}
</ul>
#endforeach
</select>
</div>
Below is my form, I am new to laravel so just trying to learn to better myself, any help is much appreciated :)
If you're using native HTML select, you may use
<div class="form-group">
<label for="role">Role</label>
<select name="roles[]" class="form-control">
#foreach($roles as $role)
<option value="{{ $role->id }}"> {{ $role->nameOrWhatever }} </option>
#endforeach
</select>
</div>
In your UserController
/**
* Show the form for creating a new user
*
* #param \App\Role $model
* #return \Illuminate\View\View
*/
public function create(Role $model)
{
return view('users.create', ['roles' => $model->get(['id', 'name'])]);
}
then, in your create.blade.php
<select name="role_id" id="input-role" required>
<option value="">-</option>
#foreach ($roles as $role)
<option value="{{ $role->id }}" {{ $role->id == old('role_id') ? 'selected' : '' }}>{{ $role->name }}</option>
#endforeach
</select>
This will get you the desired
On the Edit view I have a Select field and I want that select field to have the saved value for a given Model to be selected.
mediaController.php
public function edit($id)
{
//
$media = Media::find($id);
$categories = Category::lists('category', 'id');
return view('medias.edit-media')->with('media', $media)->with('categories', $categories);
}
edit.blade.php
<div class="form-group">
{!! Form::select('categories', $categories, $media->category ) !!}
</div>
On the index view (i.e the first Media as a Category of Video)
On the edit view (the first Media doesn't have the Category 'Video' selected)
even if I change my edit.blade.php to this: ...
<div class="form-group">
<label>Category of Upload
<select name="category" id="category" class="form-control input-sm">
#foreach($categories as $category)
<option value="{{ $category }}" {{ Input::old($media->category) == $category ? 'selected' : '' }}>{{ $category }}</option>
#endforeach
</select>
</label>
</div>
I still have the same result (the right Category is not selected)
<div class="form-group"><label>Category of Upload <select name="category" id="category" class="form-control input-sm"> #foreach($categories as $category) <option value="{{ $category->id }}" {{ $media->categories == $category->id ? 'selected' : '' }}>{{ $category }}</option>#endforeach </select> </label></div>