Home > database >  laravel passing parameters to route in view blade
laravel passing parameters to route in view blade

Time:08-31

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" >
            <option value=""  id="searchoption"> City </option>          
            @foreach ($cities as $city)
                <option >{{ $city->name }}</option>
            @endforeach
        </select>
        <div >
            <select name="pricefrom" >
                <option value=""  id="searchoption"> Price From </option>          
                @foreach ($pricefrom as $low)
                    <option >{{ $low->price}}</option>
                @endforeach
            </select>
        </div>
        <div >
            <select name="priceto" >
                <option value=""  id="searchoption"> Price To </option>          
                @foreach ($priceto as $high)
                    <option >{{ $high->price}}</option>
                @endforeach
            </select>
        </div>
        <div >
            <button type="button" >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].

CodePudding user response:

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)

CodePudding user response:

You need add to route :

    <form action="{{ route('search.clicked',$object->first()->city_id,$model->first()->pricefrom,$model->first()->priceto) }}" method="get">

CodePudding user response:

BLADE FILE:

@foreach ($users as $villa)
    <form action="{{ route('search.clicked', $villa->city_id,$villa->pricefrom,$villa->priceto) }}" method="get">
        @csrf
        <select name="city" >
            <option value=""  id="searchoption"> City </option>          
            @foreach ($cities as $city)
                <option >{{ $city->name }}</option>
            @endforeach
        </select>
        <div >
            <select name="pricefrom" >
                <option value=""  id="searchoption"> Price From </option>          
                @foreach ($pricefrom as $low)
                    <option >{{ $low->price}}</option>
                @endforeach
            </select>
        </div>
        <div >
            <select name="priceto" >
                <option value=""  id="searchoption"> Price To </option>          
                @foreach ($priceto as $high)
                    <option >{{ $high->price}}</option>
                @endforeach
            </select>
        </div>
        <div >
            <button type="button" >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" >
            <option value=""  id="searchoption"> City </option>          
            @foreach ($villa->cities as $city)
                <option >{{ $city->name }}</option>
            @endforeach
        </select>
        <div >
            <select name="pricefrom" >
                <option value=""  id="searchoption"> Price From </option>          
                @foreach ($villa->pricefrom as $low)
                    <option >{{ $low->price}}</option>
                @endforeach
            </select>
        </div>
        <div >
            <select name="priceto" >
                <option value=""  id="searchoption"> Price To </option>          
                @foreach ($villa->priceto as $high)
                    <option >{{ $high->price}}</option>
                @endforeach
            </select>
        </div>
        <div >
            <button type="button" >search</button>
        </div>
    </form>
@endforeach
  • Related