对于我的数据库table,虽然下面的代码工作正常:
$sql = DB::table('table')->select('id')->orderBy('id', 'asc')->limit(50)->get();
有 50 行,这不会:
$sql = DB::table('table')->select('id');
$sql->orderBy('id', 'asc')->limit('50')->get();
因为它确实返回了正确的行,但它返回了数据库本身中的所有行。我需要能够将它分开,因为我打算在两者之间使用 if 语句来WHERE根据用户输入添加子句,如下所示(编辑:添加的代码类似于实际代码):
$foo = $request->input('foo');
$bar = $request->input('bar');
$sqls = DB::table('foobar')->select('id', 'name', 'age', 'zip_code');
if($foo === "foo") $sqls->where('age', '<', 31);
if($bar === "bor") $sqls->where('zip_code', '>', '12345');
$sqls->orderBy('age', 'asc')->limit(50)->get();
$return = '';
$sqls->each(function($sql) use (&$return){
$return .= "<td> {$sql->id} </td>"
}
返回所有行而不是限制为 50 行。
手掌心
鸿蒙传说