一,得到最新的sql
-
DB::connection()->enableQueryLog();
//这里为查询操作
-
dump(DB::getQueryLog());
上面方法参数不能直接打印出来,可以定义一个全局函数,然后将上面的 dump(DB::getQueryLog()) 替换成
dump(getRealSql(DB::getQueryLog()));
/**
* 打印完整Sql
*/
if (! function_exists('getRealSql')) {
/**
* @param $message
*/
function getRealSql($bindings)
{
$data = [];
foreach ($bindings as $k=> $b){
$bi = $b['bindings'];
$bi = array_map(function($a){
return '\''.$a.'\'';
},$bi);
$sql = str_replace('?', '%s', $b['query']);
$sql2 = sprintf($sql, ...$bi);
$data[$k] = $sql2;
}
return $data;
}
}
二,更新表单数据
因为含有_token
可以:
$data = request()->except(['_token']);
Model::where('id',$request->input('id'))->update($data);
也可以:
Model::find($request->input('id'))->update($request->all());
想要实现laravel多条件查询 嵌套查询:
SELECT
`price`
FROM
`fedexprices`
WHERE
( `profile` = 'FEDEX101' AND `service` = 'SMART_POST' AND `weight` = '50.0' AND `zone` = '2' )
AND ( `start_at` <= '2019-09-25' OR `start_at` IS NULL )
ORDER BY
`start_at` DESC
LIMIT 1
前面可以组装成
$where = array(
['profile', '=', 'p'],
['service', '=', 's'],
['weight', '=', $totalWeight],
['zone', '=', $realZone]
);
需要嵌套的时候使用回调函数,回调函数相当于加个小括号在外面
$hasStartAt = Model::where($where)->where(function ($query) {
$query->where('start_at', '<=', date('Y-m-d'))
->orWhere('start_at', null);
})->select($select)->orderByDesc('start_at')->first();
##查询另外的表的子查询举例
User::whereIn('id', function($query){
$query->select('user_id')
->from('admin_user')
->whereIn('type', ['1', '2']);
})->get();
参考:https://blog.csdn.net/xcqingfeng/article/details/80364618
三,在验证后手动填值:
一般表单验证后会自动存之前的数据,也可以手动更改,使用withInput,
return redirect()-> back()->withInput(['error'=>'注册失败,短信验证码不正确','page'=>'phone']);
页面也可以用old展示
<input type="text" name="username" value="{{ old('error') }}">
四, 得到某个表格的所有字段:
Schema::getColumnListing('xxxxs');
Schema的命名空间: Illuminate\Support\Facades\Schema