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

问题描述 投票:0回答:1

我正在尝试在本教程的帮助下在我的 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
        ));
    }
}

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

php laravel laravel-5 search-engine
1个回答
1
投票

基于ProductCategory模型之间的关系,您必须在代表您的关系的Category模型中定义product()函数。检查此链接

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

类别型号 -

<?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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.