我对where
命令有疑问。我在我的数据库表中有这个表单元素,有很多列,我需要搜索特定的值:customers和type。如下图所示......
<form action="{{ route(shop.find) }}">
<select class="form-control" name="customers1" id="customers1">
@foreach ($customers as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
</select>
<select class="form-control" name="type1" id="type1">
@foreach ($types as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
</select>
</form>
在我的控制器中,我被困在where
命令。
public function find(Request $request){
$customers = DB::table("tbl_customers")->pluck('name','id')->where('name', '=', $request->name);
//This where command is absolutely wrong. I need the right ways to do it.
$types = DB::table("tbl_types")->pluck('race','raceid')->where('race', '=', $request->race);
return view('shop.find',compact('customers', 'types'));}
我不知道自己需要什么。或者我需要使用什么。我希望你们能帮忙。
答案 0 :(得分:1)
执行pluck()->where()
时,您将加载所有行,然后使用该集合。正确的语法是:
public function find(Request $request)
{
$customers = DB::table("tbl_customers")->where('name', $request->name)->pluck('name', 'id');
$types = DB::table("tbl_types")->where('race', $request->race)->pluck('race', 'raceid');
return view('shop.find', compact('customers', 'types'));
}