方法Illuminate \ Database \ Query \ Builder :: filter不存在 [英] Method Illuminate\Database\Query\Builder::filter does not exist
问题描述
我正在按照一个教程编写两个用于过滤论坛应用程序中的线程的类.我在此在线得到此错误
$ threads = Thread :: latest()-> filter($ filters); (在threadscontroller中)
I am following a tutorial to write 2 classes for filtering threads in a forum application.I got this error in line
$threads = Thread::latest()->filter($filters); (in threadscontroller)
方法Illuminate \ Database \ Query \ Builder :: filter不存在.
Method Illuminate\Database\Query\Builder::filter does not exist.
带有索引方法的ThreadsController:
ThreadsController with index method:
<?php
namespace App\Http\Controllers;
use App\Thread;
use App\Channel;
use App\Filters\ThreadFilters;
use Illuminate\Http\Request;
class ThreadsController extends Controller
{
public function __construct(){
$this->middleware('auth')->only('store','create');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Channel $channel,ThreadFilters $filters)
{
$threads = Thread::latest()->filter($filters);
if($channel->exist){
$threads->where('channel_id',$channel->id);
}
$threads = $threads->get();
return view('threads.index',compact('threads'));
}
这是抽象类Filters:
This is the abstract class Filters:
<?php
namespace App\Filters;
use Illuminate\Http\Request;
abstract class Filters{
protected $request;
protected $builder;
protected $filters = [];
public function __construct(Request $request){
$this->request = $request;
}
public function apply($builder){
$this->builder = $builder;
foreach($this->getFilters() as $filter=>$value){ //filter by,value yunus mesela.
if(method_exist($this,$filter)){
$this->$filter($value);
}
}
return $this->builder;
}
public function getFilters(){
return $this->request->intersect($this->filters);
}
}
这里ThreadFilters.php扩展了过滤器类:
Here ThreadFilters.php which extends filters class:
<?php
namespace App\Filters;
use App\User;
use Illuminate\Http\Request;
class ThreadFilters extends Filters
{
protected $filters =['by'];
protected function by($username){
$user = User::where('name',$username)->firstorFail();
return $this->builder->where('user_id',$user->id);
}
}
如果我对所有内容都进行了最新更改,则会收到以下错误消息: 类型错误:传递给Illuminate \ Support \ Collection :: filter()的参数1必须是可调用的或为null,已给定对象,在
If i change latest to all,i get this error: Type error: Argument 1 passed to Illuminate\Support\Collection::filter() must be callable or null, object given, called in
还有谁能解释我在这些课程中$ builder在做什么?
Also can anyone explain me what is $builder doing in those classes ?
推荐答案
latest()
是修饰符快捷方式,等效于orderBy('created_at', 'desc')
.它所做的只是向查询添加ORDER BY
约束.
latest()
is a modifier shortcut, equivalent to orderBy('created_at', 'desc')
. All it does is add the ORDER BY
constraint to the query.
filter()
是Collection类上的方法.该方法在查询生成器中不存在,因此,您将收到找不到方法"错误.
filter()
is a method on the Collection class. That method does not exist in the query builder, hence the "method not found" error you're receiving.
似乎没有将过滤器类与结果Collection一起使用.而是,它向您的原始查询添加条件.尝试像这样实现它:
It does not appear that your filter class should be used with the resulting Collection. Rather, it adds conditionals to your original query. Try implementing it like this:
// Remove the filters() method here.
$threads = Thread::latest();
if ($channel->exist) {
$threads->where('channel_id', $channel->id);
}
// Pass your query builder instance to the Filters' apply() method.
$filters->apply($threads);
// Perform the query and fetch results.
$threads = $threads->get();
此外,对于将来的问题,包括您正在尝试/遵循的教程,可以为那些帮助您的人提供有益的背景. :)
Also, for future questions, including the tutorial you're attempting/following can provide beneficial context to those helping you. :)
这篇关于方法Illuminate \ Database \ Query \ Builder :: filter不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!