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

时间:2016-06-18 03:52:07

标签: laravel laravel-5.2 search-engine

我正试图在我的Laravel 5中使用本教程实施智能搜索引擎 https://maxoffsky.com/code-blog/laravel-shop-tutorial-3-implementing-smart-search/
我改变了一些代码因为laravel 4的本教程 现在我被困在这里当我输入任何像杯子这样的关键词时,我的删除器工具中的网络选项卡上出现了错误

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

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

1 个答案:

答案 0 :(得分:1)

根据您在产品类别模型之间的关系,您必须在类别模型中定义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
    }
}