Laravel双肩包如何反弹到精确匹配,而不是在表格中仅显示一个结果

Laravel双肩包如何反弹到精确匹配,而不是在表格中仅显示一个结果,laravel,laravel-5,backpack-for-laravel,Laravel,Laravel 5,Backpack For Laravel,在我的Laravel5.7应用程序中,我目前浏览URL,例如使用,这样我就可以直接搜索联系人表,而无需在DataTables ajax搜索字段中键入 这工作得很好,除了我更喜欢它直接跳转到编辑页面,以获得仅1个结果的精确匹配 在ContactCrudController设置中,我有: 但这不起作用,因为安装程序不希望返回重定向 如何实现我的目标?尝试在控制器的构造函数中使用: class ContactCrudController extends Controller { /**

在我的Laravel5.7应用程序中,我目前浏览URL,例如使用,这样我就可以直接搜索联系人表,而无需在DataTables ajax搜索字段中键入

这工作得很好,除了我更喜欢它直接跳转到编辑页面,以获得仅1个结果的精确匹配

在ContactCrudController设置中,我有:

但这不起作用,因为安装程序不希望返回重定向

如何实现我的目标?

尝试在控制器的构造函数中使用:

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);
        });
    }
}
尝试在控制器的构造函数中使用:

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);
        });
    }
}
看一看这张照片。您可以使用它截获来自服务器的响应,并基于它重定向

我可以想象在服务器端会出现这样的情况:

function datatableData()
{
    $q = $this->request->query->get('q');

    $contacts = \App\Models\Contact::query()
        ->when($q, function ($query, $q) { // apply filter only if present
             $query->where('emailAddress', $q);
        })->get();

    if ($contacts->count() === 1) {
        $id = $contacts->first()->id;
        return response()->json(['redirect' => url("/admin/contact/$id/edit")]);
    }

    return response()->json($contacts); // or whatever you return normally
}
以及客户端的以下内容:

var dt = $('#example').dataTable(...yourConfig...);

dt.on('xhr.dt', function (e, settings, json, xhr) {
    if (json.redirect) {
        window.location.href = json.redirect;
    }
});
看一看这张照片。您可以使用它截获来自服务器的响应,并基于它重定向

我可以想象在服务器端会出现这样的情况:

function datatableData()
{
    $q = $this->request->query->get('q');

    $contacts = \App\Models\Contact::query()
        ->when($q, function ($query, $q) { // apply filter only if present
             $query->where('emailAddress', $q);
        })->get();

    if ($contacts->count() === 1) {
        $id = $contacts->first()->id;
        return response()->json(['redirect' => url("/admin/contact/$id/edit")]);
    }

    return response()->json($contacts); // or whatever you return normally
}
以及客户端的以下内容:

var dt = $('#example').dataTable(...yourConfig...);

dt.on('xhr.dt', function (e, settings, json, xhr) {
    if (json.redirect) {
        window.location.href = json.redirect;
    }
});
以下是我为Laravel 6.x和Backpack 4更新的方法: 在ContactCrudController中:

public function __construct() {
    parent::__construct();
    $this->redirectIfExactMatch();
}

/**
 * @see https://stackoverflow.com/a/53771897/470749
 */
public function redirectIfExactMatch() {
    $this->middleware(function ($request, $next) {
        $q = $request->input(self::Q);
        if ($q) {
            $contact = \App\Models\Contact::where('emailAddress', $q)->first();
            if ($contact) {// if there is an exact email match, redirect to the Edit page of that Contact.
                return redirect($contact->getContactEditUrl());
            }
        }
        return $next($request);
    })->only('index'); //https://laravel.com/docs/6.x/controllers#controller-middleware
}
我使用->only“index”将中间件仅应用于index函数

另请参见

以下是我为Laravel 6.x和Backpack 4更新的方法: 在ContactCrudController中:

public function __construct() {
    parent::__construct();
    $this->redirectIfExactMatch();
}

/**
 * @see https://stackoverflow.com/a/53771897/470749
 */
public function redirectIfExactMatch() {
    $this->middleware(function ($request, $next) {
        $q = $request->input(self::Q);
        if ($q) {
            $contact = \App\Models\Contact::where('emailAddress', $q)->first();
            if ($contact) {// if there is an exact email match, redirect to the Edit page of that Contact.
                return redirect($contact->getContactEditUrl());
            }
        }
        return $next($request);
    })->only('index'); //https://laravel.com/docs/6.x/controllers#controller-middleware
}
我使用->only“index”将中间件仅应用于index函数


另请参见

谢谢!它几乎成功了,然后我添加了parent::\uu construct;,成功了,谢谢你!它几乎成功了,然后我添加了parent::\uu construct;,它成功了。谢谢你的回答。我选择了另一种方法,因为它更熟悉。谢谢你的回答。我选择了另一种方法,因为它更熟悉。