简体   繁体   English

laravel 将参数传递给视图刀片中的路由

[英]laravel passing parameters to route in view blade

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!所以我对 laravel 非常陌生,我的网站刀片中有一个过滤器,如下所示:目标:用户选择一个城市和价格,然后点击搜索,选择的别墅必须显示在另一个视图上! 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: 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还有别墅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]. [Route: search.clicked] [URI: searched/{city_id}/{pricefrom}/{priceto}] [缺少参数: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.根据路由 function 第二个参数应该是字符串或数组。 String or Array if you're passing only 1 & array if you are passing 2 or more.如果您仅传递 1 个字符串或数组,如果您传递 2 个或更多,则为数组。

 function route($name, $parameters = [], $absolute = true)

You need add to route:您需要添加到路线:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM