mardi 17 avril 2018

search engine laravel and vue.js without scout

Hi am trying to make search engine with laravel and vue.js but i have no result: this is my SearchController.php

namespace Amp\Http\Controllers;

use Amp\User;
use Illuminate\Http\Request;

class SearchController extends Controller
{

    /**
     * @param Request $request
     * @return array
     */
    public function search(Request $request)
    {

        $error = ['error' => 'No results found, please try with different keywords.'];

        if ($request->has('q')) {

             $users = User::search($request->get('q'))->get();
            return $users->count() ? $users : $error;

        }

        return $error;

    }
}

this my TopNavbar.vue:

<template>
    <div>
        <input type="text" v-model="keywords">
        <ul v-if="results.length > 0">
            <li v-for="result in results" :key="result.id" v-text="result.name"></li>
        </ul>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        data() {
            return {
                keywords: null,
                results: []
            };
        },

        watch: {
            keywords(after, before) {
                this.fetch();
            }
        },

        methods: {
            fetch() {
                axios.get('api/search', { params: { keywords: this.keywords } })
                    .then(response => this.results = response.data)
                    .catch(error => {});
            }
        }
    }
</script>

If i use only the api url then i have result and work proprely i mean if i make search with url on the browser something like this: api/search?q=XXXX then work pefect but only on browser wen i try to make search on then nothing thank you for your help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire