Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 方法\Database\Query\Builder::筛选器不存在_Php_Laravel - Fatal编程技术网

Php 方法\Database\Query\Builder::筛选器不存在

Php 方法\Database\Query\Builder::筛选器不存在,php,laravel,Php,Laravel,我按照教程编写了两个类来过滤论坛应用程序中的线程 $threads = Thread::latest()->filter($filters); // in threadscontroller 错误: <?php namespace App\Filters; use Illuminate\Http\Request; abstract class Filters{ protected $request; protected $builder; protected $filte

我按照教程编写了两个类来过滤论坛应用程序中的线程

 $threads = Thread::latest()->filter($filters); // in threadscontroller
错误

<?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); 
    } 

}
方法\Database\Query\Builder::筛选器不存在

螺纹控制器带索引方法:

<?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'));


     
    }
latest()
是一个修改器快捷方式,相当于
orderBy('created_at','desc')
。它所做的只是向查询中添加
orderby
约束

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

您的筛选器类似乎不应与生成的集合一起使用。相反,它会将条件添加到原始查询中。试着这样实现它:

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

此外,对于未来的问题,包括您正在尝试/遵循的教程,可以为帮助您的人提供有益的背景信息。:)

如果您将“最新”更改为“所有”,您将获得一个Laravel系列。因此,您正在对集合调用
filter()
$threads=Thread::all()->filter($filters);

如果查看,您将看到,数组类的
where()
方法被调用,它调用PHP的方法。正如你所看到的,必须给出一个答案。 但是您正在将一个对象传递给filter方法,
$filters
,这是一个ThreadFilters对象->方法注入:
public函数索引(Channel$Channel,ThreadFilters$filters).

您的错误消息很好地回答了您的问题:
类型错误:传递给illumb\Support\Collection::filter()的参数1必须是可调用的或空的,给定的对象,在

中调用提示:我的答案只回答您的第二条错误消息:)我想getFilters方法还有另一个问题。方法Illumb\Http\Request::intersect不存在。顺便说一句,我正在学习laracasts教程
Request::intersect()
已在L5.5中删除。请尝试仅使用
($this->filters)
。当我尝试使用以下用户名进行筛选时,您好:我调用了未定义的函数App\filters\method\u exist()
// 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();