方法Illuminate \ Database \ Query \ Builder::filter不存在 - php

我正在按照一个教程编写一个用于过滤论坛应用程序中的线程的2个类。
$ threads = Thread :: latest()-> filter($ filters); (在threadscontroller中)

方法Illuminate \ Database \ Query \ Builder :: filter不存在。

带有索引方法的ThreadsController:

<?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:

<?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扩展了过滤器类:

<?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,已给定对象,在

还有谁能解释我在这些课程中$ builder在做什么?

参考方案

latest()是修饰符快捷方式,等效于orderBy('created_at', 'desc')。它所做的只是向查询添加ORDER BY约束。

filter()是Collection类的方法。该方法在查询生成器中不存在,因此,您将收到“找不到方法”错误。

似乎没有将过滤器类与结果Collection一起使用。相反,它将条件添加到原始查询中。尝试像这样实现它:

// 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();

此外,对于将来的问题,包括您正在尝试/跟随的教程,可以为帮助您的人员提供有益的背景。 🙂

如何从DOMXPath::query()方法获取完整的HTML? - php

我有要从中提取未删除内容的特定div的文档。我做:$dom = new DOMDocument(); $dom->loadHTML($string);//that's HTML of my document, string 和xpath查询:$xpath = new DOMXPath($dom); $xpath_resultset = $xp…

是否可以阻止用户下载SQLite文件? - php

我正在从here学习SQLite3;它确实很好并且可移植,但是如果有人以某种方式知道数据库文件名test.db然后直接下载它怎么办?它可能比SQL注入更为危险,因为攻击者可以轻松获取整个数据库的副本。 参考方案 您可以限制.db文件中的.htaccess文件以执行相同的操作,将这行代码添加到位于根目录中的.htaccess文件中<Files ~ �…

PHP-无法动态实例化类 - php

突然,我无法动态实例化一个类。如果直接调用它,我可以实例化它,但是用变量调用它是行不通的。这是行不通的: $database_class = 'MysqlConnection'; $class = new MysqlConnection(); $other_class = new $database_class(); 第一个实例化为$cl…

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

php ziparchive类源代码 - php

Improve this question 我如何获取ziparchive类本身的源代码。 参考方案 假设您在谈论PHP ZipArchive class:下载PHP source code并查找适当的文件。如果您希望源代码是PHP代码,您可能会感到失望,因为源代码是用C语言编写的。或者,也可以在PHP Github Development Reposito…