laravel - Laravel Backpack 如何反弹到精确匹配而不是在表格中只显示 1 个结果
问题描述
在我的 Laravel 5.7 应用程序中,我目前使用“q”参数浏览到诸如https://example.com/admin/contact?q=john@example.com这样的 URL,这样我就可以直接搜索联系人表而无需在 DataTables ajax 搜索字段中键入。
这很好用,只是我希望它直接跳到编辑页面(对于只有 1 个结果的完全匹配)。
在我的ContactCrudController
setup()
,我有:
$q = $this->request->query->get('q');
if ($q) {// if there is an exact email match, redirect to the Edit page of that Contact.
$matchingContact = \App\Models\Contact::where('emailAddress', $q)->first();
if ($matchingContact) {
return redirect(url('/admin/contact/' . $matchingContact->id . '/edit'));
}
}
但这不起作用,因为setup()
不期望return redirect()
.
我怎样才能实现我的目标?
解决方案
尝试在控制器的构造函数中使用中间件:
class ContactCrudController extends Controller
{
/**
* Instantiate a new controller instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->middleware(function ($request, $next) {
if ($contact = \App\Models\Contact::where('emailAddress', $request->query->get('q'))->first()) {
return redirect(url('/admin/contact/' . $contact->id . '/edit'));
}
return $next($request);
});
}
}
推荐阅读
- javascript - 如何在angular 2材料表的搜索结果中加粗搜索模式
- python - Django Rest Framework - 在空值上调用 serializer.to_internal_value
- vba - 如何将结果集中的值分配给表单中的字段?
- r - 在r中将标识符变量从一个数据集中拉到另一个数据集中
- java - 使用 Java 的 Domino 数据库连接问题
- python - 在 Pandas 数据框中查找符合特定条件的数字序列
- python - 最多期望输入 1 个参数,得到 2 个
- jquery - css left 导致元素显示为内联
- c++ - 有人可以解释一下这段代码吗?它给出了我们输入的数字的倒数
- php - 如何打破 sql 查询无限循环