[英]laravel - option all values
嗨,我想知道如何在“選擇”中創建一個“選項”,以獲取該“選擇”的所有可能值。 我嘗試了whereIn和whereBetwen,但無法獲得。 甚至有人提到使用Jquery。 laravel版本是5.4。
這是代碼:
調節器
function catalog(Request $abc) {
$categoryesc = $abc->categoryesc;
$manufaesc = $abc->manufaesc;
$priceesc = $abc->priceesc;
$modelesc = $abc->modelesc;
if (empty($modelesc)) {
$robots = DB::table('robots')->where('Category_id', [$categoryesc])->where('Manufacturer_id', [$manufaesc])->where('Price', '<=', $priceesc)->get();
}elseif (Auth::check()){
$robots = DB::table('robots')->where('Model', $modelesc)->first();
$colours = DB::table('colours')->pluck('Colour', 'id');
}
else {
return redirect('login');
}
return view('catalog', compact('robots', 'categories', 'colours'));
}
刀片文件:
{{ Form::open(array('method' => 'GET')) }}
{{ Form::select('categoryesc', ['0' => 'Any Category', '1' => 'Light', '2' => 'Medium', '3' => 'Heavy'], '0') }}
{{ Form::select('manufaesc', ['0' => 'Any Make', '1' => 'Xenon', '2' => 'Tauron'], '0') }}
{{ Form::select('priceesc', ['0' => 'Any Price', '100' => '100', '200' => '200'], '0') }}
{{ Form::submit('Search') }}
{{ Form::close() }}
錯誤:
ErrorException in 164eed4705c5997d268ffab821c82effa7651a2b.php line 31:
Undefined property: Illuminate\Database\MySqlConnection::$Model (View: C:
(...)\resources\views\catalog.blade.php)
in 164eed4705c5997d268ffab821c82effa7651a2b.php line 31
at CompilerEngine->handleViewException(object(ErrorException), 1) in
PhpEngine.php line 44
at PhpEngine->evaluatePath('C:\\(...) \\ storage\\ framework\\ views/
164eed4705c5997d268ffab821c82effa7651a2b.php', array('__env' =>
object(Factory), 'app' => object(Application), 'errors' =>
object(ViewErrorBag), 'robots' => object(Builder))) in CompilerEngine.php
line 59
at CompilerEngine->get('C:(...)\\resources\\views/catalog.blade.php',
array('__env' => object(Factory), 'app' => object(Application), 'errors' =>
object(ViewErrorBag), 'robots' => object(Builder))) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
與其嘗試提出一種包括所有此類內容的方法,不如對其進行更改以根據您的選擇限制選擇范圍。 所以
$robots = DB::table('robots')->where('Category_id', [$categoryesc])->where('Manufacturer_id', [$manufaesc])->where('Price', '<=', $priceesc)->get();
可以寫成
$robots = DB::table('robots');
if ( $categoryesc !=0 ) {
$robots->where('Category_id', $categoryesc);
}
if ( $manufaesc!=0 ) {
$robots->where('Manufacturer_id', $manufaesc);
}
if ( $manufaesc!=0 ) {
$robots->where('Price', '<=', $priceesc);
}
$robots->get();
然后在刀片中,將“任何”值設置為0。這樣,它僅在需要時才包含選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.