PHP-003 / 中级
向 orderBy 传入用户提供的列名
为什么存在问题?
列名等标识符无法作为参数绑定,需要允许列表,也可能发生子查询注入。
如何修复
php
$allowed = ['created_at', 'title'];
$sort = in_array($request->sort, $allowed, true) ? $request->sort : 'created_at';
$posts = Post::orderBy($sort, 'desc')->get();