[英]laravel passing parameters to route in view blade
所以我对 laravel 非常陌生,我的网站刀片中有一个过滤器,如下所示:目标:用户选择一个城市和价格,然后点击搜索,选择的别墅必须显示在另一个视图上! 数据库中的所有值!
我试过这样做:
路线:
Route::get('/searched/{city_id}/{pricefrom}/{priceto}', [WebsiteController::class, 'search'])->name('search.clicked');
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);
}
网站视图刀片:在这里我不知道我应该如何准确地传递路由的参数,所以我得到一个错误
@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
还有别墅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');
}
}
我想我有很多错误需要修复我需要一些关于我的搜索栏的帮助!
我收到此错误:
[Route: search.clicked] [URI: searched/{city_id}/{pricefrom}/{priceto}] [缺少参数:pricefrom, priceto] 缺少必需的参数。
尝试:
<form action="{{ route('search.clicked', [$villa->city_id,$villa->pricefrom,$villa->priceto]) }}" method="get">
根据路由 function 第二个参数应该是字符串或数组。 如果您仅传递 1 个字符串或数组,如果您传递 2 个或更多,则为数组。
function route($name, $parameters = [], $absolute = true)
您需要添加到路线:
<form action="{{ route('search.clicked',$object->first()->city_id,$model->first()->pricefrom,$model->first()->priceto) }}" method="get">
刀片文件:
@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
至
@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.