lundi 29 octobre 2018

Laravel, Vuejs redirect with params

I have an option to search for users, what i would like to achieve is for the search to return an array with users to a new page. I made this work with window.location after my request but that crashes when there is to many users and not a way i would like to achieve this.

If you know any other way to tackle this please let me know.

Here is what i am currently doing:

Search.vue:

<template>
 <input type="text" v-model="query" @keyup.enter="search()"/>
 <button @click="search()"  type="button">
</template>


<script>
export default {
    data() {
        return {
            users: [],
            query: ''
        }
    },
    methods: {
        search: function() {
            this.users = [];
            this.loading = true;
            axios.get('/api/search?q=' + this.query).then((response) => {
             this.users = response.data;
               this.loading = false;
               this.query = '';
               window.location = '/search/?users=' + JSON.stringify(this.users);
            });
        }
    }
};
</script>

SearchController.php:

class SearchController extends Controller
{
    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()->load('stats');
            return $users->count() ? $users : $error;
        }
        return $error;
    }
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OaIE0p
via IFTTT

Aucun commentaire:

Enregistrer un commentaire