![](/img/trans.png)
[英]LARAVEL Call to undefined method Illuminate\Database\Eloquent\Builder::splice()
[英]Call to undefined method Illuminate\Database\Eloquent\Builder::search() using laravel scout and tntsearch
在我的 larave-vue 应用程序中,我希望能够使用 laravel scout tntsearch 按搜索词进行过滤,此外,我还在 controller 中执行额外的过滤、排序或 where 子句。 我无法让两者都在我的应用程序中正常工作。
在我的 controller 我有这个:
$posts = Post::filter($filters)->search($request->input('query'))->paginate(0);
$posts->load(['postcategory.section','author']);
if($posts->isEmpty())
{
return response()->json([
'message' => 'No results found',
],500);
}
return response()->json([
'message' => 'Results found',
'posts' => $posts,
]);
它给了我以下错误:
Call to undefined method Illuminate\Database\Eloquent\Builder::search()
由于某种原因它不起作用,我尝试更改过滤器、搜索和分页的顺序,但仍然出现错误。
如果您想知道我的过滤方法是如何工作的,它基本上是 scope
您可以在此处阅读有关它的深入文章:
https://medium.com/@mykeels/writing-clean-composable-eloquent-filters-edd242c82cc8
以防万一,请参阅下面的 QueryFilters 和 Filterable 特征
<?php
namespace App\Filters;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
class QueryFilters
{
protected $request;
protected $builder;
public function __construct(Request $request)
{
$this->request = $request;
}
public function title($term)
{
$lowerCaseTerm = strtolower($term);
return $this->builder->where('title', 'LIKE', "%$lowerCaseTerm%");
}
public function postCategory($term)
{
return $this->builder->whereHas('postcategory', function ($query) use ($term){
$query->where('id', $term);
});
}
public function sort($term)
{
$sortArray = explode(",", $term);
for($i = 0; $i <= $sortArray.length; $i++)
{
$sortBy = substr_replace($sortArray[i], "", -1);
$sortChar = substr($sortArray[i], -1);
$sortOrder = $sortChar == '+' ? 'ASC' : 'DESC';
$this->bulider->orderBy($sortBy, $sortOrder);
}
return $this->builder;
}
public function apply(Builder $builder)
{
$this->builder = $builder;
foreach ($this->filters() as $name => $value)
{
//if method doesn't exists continue out of the loop
if ( ! method_exists($this, $name))
{
continue;
}
//method exists so check if it has a value payload so call the method with arguments
if (strlen($value))
{
$this->$name($value);
}
//it doesn't have a payload so call the method without arguments
else
{
$this->$name();
}
}
return $this->builder;
}
public function filters()
{
//returns associative array of request body key value pairs
return $this->request->all();
}
}
和可过滤的特征
<?php
namespace App\Filters;
use Illuminate\Database\Eloquent\Builder;
trait Filterable
{
public function scopeFilter($query, QueryFilters $filters)
{
return $filters->apply($query);
}
}
search() 方法不会添加到现有查询中,这是通过可搜索特征添加到 model 的方法
public static function search($query = '', $callback = null)
{
return app(Builder::class, [
'model' => new static,
'query' => $query,
'callback' => $callback,
'softDelete'=> static::usesSoftDelete() && config('scout.soft_delete', false),
]);
}
function 是 static,它不以查询构建器为参数,无论如何都会返回一个新的构建器。 因此,您不能将搜索链接到查询,但您可以从搜索开始,然后应用您的过滤器
所以代替这个
$posts = Post::filter($filters)->search($request->input('query'))->paginate(0);
尝试这个
$posts = Post::search($request->input('query'))->filter($filters)->paginate(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.