简体   繁体   中英

laravel - option all values

Hi i would like to know how to create an "option" in a "select" that gets all the possible values of that "select". I have tried whereIn and whereBetwen, but cant get it. Someone even mentioned using Jquery for it. The laravel version is 5.4.

This is the code:

controller

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')); 
}

blade file:

{{ 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() }}

Error:

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

Rather than trying to come up with a way of including everything like this, you can change it to limit the selections according to what you select. So

$robots = DB::table('robots')->where('Category_id', [$categoryesc])->where('Manufacturer_id', [$manufaesc])->where('Price', '<=', $priceesc)->get();

Could be written as

$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();

And in your blade, you set the 'Any' value to 0. So that it only includes a selection when you need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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