Php Laravel Select2搜索

Php Laravel Select2搜索,php,ajax,eloquent,jquery-select2,Php,Ajax,Eloquent,Jquery Select2,我使用select2 jquery插件,但无法搜索要输出的数据。 我一直试图在我的数据库中搜索一本书的名字,这本书应该是可用的,我现在在公司分公司。我要处理两张桌子 书籍 book_type_id [1, 2] status ['Available', 'Available'] 图书类型 id [1, 2] name ['Book 1', 'Book 2'] 及 这将导致“未找到结果”。然而,当我使用 $book_type = $bt->where('book_type.name',

我使用select2 jquery插件,但无法搜索要输出的数据。 我一直试图在我的数据库中搜索一本书的名字,这本书应该是可用的,我现在在公司分公司。我要处理两张桌子

书籍

book_type_id [1, 2]

status ['Available', 'Available']
图书类型

id [1, 2] name ['Book 1', 'Book 2']

这将导致“未找到结果”。然而,当我使用

$book_type = $bt->where('book_type.name', $request->name);
它返回一个数据,所以我相信我的初始查询是正确的,不是空的,但这不是我想要的,因为我使用它进行搜索,我不希望我的用户键入整个单词以将其输出到select2


如果我不使用关系,
$book\u type
like查询可以工作。

您可以使用
whereHas
方法过滤关系:

$book_type = books::with('book_type')
             ->where('branch_id', $employee->branch_id)
             ->where('status', 'Available')
             ->where('id', 'LIKE', '%'.$request->name.'%')
             ->whereHas('book_type', function($query) use ($request) {
                 $query->where('book_type.name', 'LIKE', '%'.$request->name.'%');
             }
             ->groupBy('book_type_id')->get();
$book_type = books::with('book_type')
             ->where('branch_id', $employee->branch_id)
             ->where('status', 'Available')
             ->where('id', 'LIKE', '%'.$request->name.'%')
             ->whereHas('book_type', function($query) use ($request) {
                 $query->where('book_type.name', 'LIKE', '%'.$request->name.'%');
             }
             ->groupBy('book_type_id')->get();