我没有得到关于从视图中的搜索表单中获取俱乐部的功能的结果。
在这里,我的"搜索"控制器
public updateBestScore(scoreBD: ScoreBD, level: string): Promise<any> {
return client.connect(url)
.then(
// Promesse tenue
(db: any) => db.collection('BEST_SCORES').findOneAndUpdate(
{ "level": level.toLowerCase, "bestScore.username": scoreBD.usernameBD },
{ $set: { "bestScore.$.score": scoreBD.score, "bestScore.$.username": scoreBD.usernamePlayer} },
{ returnNewDocument : true }
)
.then(
(res: any) => {
db.close();
console.log("resultat : ", res);
if (res.lastErrorObject.updatedExisting === true) {
console.log("resultat apres findOneAndUpdate : ", res.value.bestScore);
}
}),
// Promesse rompue
(err: any) => { throw "erreur"; });
}
我试图录制一个俱乐部的确切名称,但仍然没有结果,有人有想法获得一个有用的搜索方法吗?提前谢谢
答案 0 :(得分:1)
您传递的整个请求会在搜索中将$ request更改为$ request-&gt;输入(&#39; recherche&#39;)或$ request-&gt; recherche
公共职能getClubs(请求$请求){
if($request->has('recherche')){
$clubs = Structure::where('nom_structure' , 'LIKE', '%'. $request->recherche .'%')->where('type_structure_id' , '=' , 4)->get();
}else{
$clubs = Structure::where('type_structure_id' , 4)->paginate(10);
}
dd($clubs);
return view('structure/clubs' , compact('clubs'));
}
一个方便的调试函数是toSql(),它将显示实际的sql语句
$clubs = Structure::where('nom_structure' , 'LIKE', '%'. $request->recherche .'%')->where('type_structure_id' , '=' , 4)->toSql();
编辑以使用多个
$clubs = Structure::where('nom_structure' , 'LIKE', '%'. $request->recherche .'%')
->orWhere('club_name' , 'LIKE' , '%'. $request->recherche .'%')
->whereHas('categories', function ($query) use ($request) {
$query->where('category_name','LIKE' , '%'. $request->recherche .'%');
})
->where('type_structure_id' , '=' , 4)
->get();
我个人倾向于如何做到这一点
$search = $request->get('recherche');
if ($search) {
$query = Structure::Where('nom_structure', 'like', "%$search%");
} else {
$query = Structure::select();
}
$clubs = $query->where('type_structure_id', 4)->paginate(10)
->appends(['recherche' => $search]);
答案 1 :(得分:1)
在您的情况下,$request
是一个对象。您只需要输入搜索词:
$clubs = Structure::where('nom_structure' , 'LIKE', '%'. $request->input('recherche') .'%')->where('type_structure_id' , '=' , 4)->get();