【问题标题】:Building where query based on input基于输入构建where查询
【发布时间】:2016-11-21 12:46:48
【问题描述】:
matchthese=['name'=> Input::get('name'),];
Matchthose['place'=> Input::get('place')];
$Select_db = Db::table('actvities')
              ->where([[matchthese],[matchthose]])
              ->orwhere('place', Input::get('place'))
              ->orwhere('color', Input::get('color'))
              ->orwhere('people',Input::get('people'))
              ->get();

但是 orwhere 不能正常工作。我想要第一个或第二个等。我应该如何做到这一点?

【问题讨论】:

  • 第一个完美运行的地方,但是当我只选择四种颜色中的一种时,名称,放置人并想要例如,如果我选择人们去或人的地方,它会询问我的名字或给我错误的结果谁能告诉我为什么?
  • 您需要更清楚您当前的结果是什么,以及您的预期输出是什么。目前,你想要什么是模棱两可的。 “我想要第一个或第二个”你的意思是你只想要一个结果,而不是一个集合?你是什​​么意思它给你'错误的结果'。举例说明您获得的结果与预期的结果
  • 我想要一个执行查询的位置,或者每次执行一个查询时的另一个查询,具体取决于我的用户选择,这可能是位置和颜色的组合,或者只是我想要这种关系的颜色。我我不确定我的代码是否正确

标签: sql laravel where


【解决方案1】:

您可以将skip 和take 与first 一起使用:

例如第一个你可以直接使用first:

$Select_db = Db::table('actvities')
              ->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
              ->orwhere('place', Input::get('place'))
              ->orwhere('color', Input::get('color'))
              ->orwhere('people',Input::get('people'))
              ->take(1)->first();

第二个:

$Select_db = Db::table('actvities')
              ->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
              ->orwhere('place', Input::get('place'))
              ->orwhere('color', Input::get('color'))
              ->orwhere('people',Input::get('people'))
              ->skip(1)->take(1)->first();

如果你需要遍历结果,你可以简单地使用foreach:

$Select_db = Db::table('actvities')
              ->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
              ->orwhere('place', Input::get('place'))
              ->orwhere('color', Input::get('color'))
              ->orwhere('people',Input::get('people'))
              ->get();
foreach($Select_db as $record) {
    //Do something with $record.
}

希望对你有帮助。

【讨论】:

  • 非常感谢我会尝试,但我的问题是我的 orwhere 无法正常工作,我需要例如第一个正确执行的位置,但例如当我只选择输入的地方 orwhere 没有执行它问我的名字?
猜你喜欢
  • 2011-04-08
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多