调用未定义的方法 Illuminate\Database\Query\Builder::products()

Call to undefined method Illuminate\Database\Query\Builder::products()

我正尝试借助本教程
在我的 Laravel 5 中实施智能搜索引擎 https://maxoffsky.com/code-blog/laravel-shop-tutorial-3-implementing-smart-search/
我更改了一些代码,因为本教程适用于 laravel 4
现在我被困在这里当我输入诸如 cup 之类的任何关键字时,我在 deleveloper 工具的网络选项卡上出现错误

Call to undefined method Illuminate\Database\Query\Builder::products()

这是我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Product;
use App\Category;
use Response;

class ApiSearchController extends Controller
{
    public function appendValue($data, $type, $element)
    {
        // operate on the item passed by reference, adding the element and type
        foreach ($data as $key => & $item) {
            $item[$element] = $type;
        }
        return $data;       
    }

    public function appendURL($data, $prefix)
    {
        // operate on the item passed by reference, adding the url based on slug
        foreach ($data as $key => & $item) {
            $item['url'] = url($prefix.'/'.$item['slug']);
        }
        return $data;       
    }

    public function index()
    {
        $query = e(Input::get('q',''));

        if(!$query && $query == '') return Response::json(array(), 400);

        $products = Product::where('published', true)
            ->where('name','like','%'.$query.'%')
            ->orderBy('name','asc')
            ->take(5)
            ->get(array('slug','name','icon'))->toArray();

        $categories = Category::where('name','like','%'.$query.'%')
            ->has('products')
            ->take(5)
            ->get(array('slug', 'name'))
            ->toArray();

        // Data normalization
        $categories = $this->appendValue($categories, url('img/icons/category-icon.png'),'icon');

        $products   = $this->appendURL($products, 'products');
        $categories  = $this->appendURL($categories, 'categories');

        // Add type of data to each item of each set of results
        $products = $this->appendValue($products, 'product', 'class');
        $categories = $this->appendValue($categories, 'category', 'class');

        // Merge all data into one array
        $data = array_merge($products, $categories);

        return Response::json(array(
            'data'=>$data
        ));
    }
}

我的产品和类别模型是空白的,因为教程中没有内容

好吧,根据 ProductCategory 模型之间的关系,您必须在 Category 中定义 product() 函数 代表你们关系的模型。检查 This Link

例如 - 假设 一对多 关系(一个类别 - 许多产品)将是这样的 -

类别模型 -

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

    public function product()
    {
        return $this->hasMany('App\Product');
                     // ^ this will change based on relationship
    }
}

产品型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{

    public function category()
    {
        return $this->belongsTo('App\Category');
                       // ^ this will change based on relationship
    }
}