【问题标题】:search engine in laravel not workinglaravel 中的搜索引擎不工作
【发布时间】:2016-06-18 02:19:24
【问题描述】:


我正在尝试实现智能搜索引擎这是教程链接
https://github.com/msurguy/laravel-smart-search
我知道 Laravel 4 的这个教程,我想在 Laravel 5.2 中实现
现在我卡住了
我无法获取 api/search 的 json 格式
这是我的路线

Route::get('api/search', 'ApiSearchController@index');

这是我在 App/http/controller 中使用 Artisan 助手创建的控制器,我确实尝试像教程 api/searchcontroller 一样创建,但它不会总是出现错误“ApiSearchController@index Not found”

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Product;
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
        ));
    }
}

当我打开http://localhost/search/public/api/search 我应该像教程一样从数据库中获取所有数据 但不能得到
其他的东西是一样的 请告诉我怎么了

【问题讨论】:

    标签: api search laravel-5.2 search-engine


    【解决方案1】:

    您的搜索无法正常工作,因为它期望作为查询字符串或请求的一部分提供要搜索的查询。

    Input::get('q') 表示从您的查询字符串或请求中检索 q 并将其用于搜索。

    当您转到页面http://localhost/search/public/api/search 时,您是作为GET 请求进行的,因此需要像http://localhost/search/public/api/search?q=example 一样提供您的查询。

    本教程中的示例使用 AJAX 运行 GET 请求,为您完成上述操作。

    尝试将?q=xxxx 添加到您的网址末尾,如果您按照该教程进行了适当的设置,它应该可以工作。

    【讨论】:

    • 是的,为什么它不在我的 / 页面上搜索这里是我的主页输入
    • 您可以使用开发人员工具的网络选项卡检查 ajax 请求中发送的内容
    • 如何提供查询供其搜索
    • 我是否需要更多的修改,因为我是 laravel 的新手,如果你告诉我我做了什么,这将非常有帮助,谢谢
    • 哇,谢谢詹姆斯,我发现 api 错误我修复了 2 个错误,但是 Builder.php 第 2345 行中的此错误 BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::products() 我现在该怎么办
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多