gpt4 book ai didi

php - 使用 laravel scout 和 tntsearch 调用未定义的方法 Illuminate\Database\Eloquent\Builder::search()

转载 作者:行者123 更新时间:2023-12-05 03:57:20 25 4
gpt4 key购买 nike

在我的 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()

由于某种原因它不起作用,我尝试更改过滤器的顺序、搜索和分页,但我仍然遇到错误。

如果您想知道我的过滤方法是如何工作的,它基本上是一个作用域

你可以在这里阅读一篇关于它的深入文章:

https://medium.com/@mykeels/writing-clean-composable-eloquent-filters-edd242c82cc8

为了以防万一,请查看下面我的 QueryFilters 和 Filterable trait

<?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() 方法不会添加到现有查询中,这里是通过可搜索特征添加到模型中的方法

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),
]);
}

该函数是静态的,它不将查询构建器作为参数,无论如何都会返回一个新的构建器。所以你不能将搜索链接到查询上,但你可以从搜索开始,然后应用你的过滤器

所以不是这个

$posts = Post::filter($filters)->search($request->input('query'))->paginate(0);

试试这个

$posts = Post::search($request->input('query'))->filter($filters)->paginate(0);

关于php - 使用 laravel scout 和 tntsearch 调用未定义的方法 Illuminate\Database\Eloquent\Builder::search(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58626910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com