Search code examples
laravelvue.jslaravel-jetstreaminertiajs

Redirect in inertia js and jetstream in javascript code


I want to be redirected to single question page after creation of question

summitQuestion(){
    this.question_button = true
    axios.post('/api/question/ask', {
        'title' : this.question.title,
        'body' : this.question.body,
        'tags' : this.tags,
        'user_id' : this.$page.props.user.id
    }).then(response=>{
        this.question_button = false
        console.log(response.data)


    }).catch(error=>{
        this.question_button = false
        console.log(error.response.data.errors)

        if(error.response.data.errors){
            this.title_errors = error.response.data.errors.title
            this.body_errors = error.response.data.errors.body
        }


    })
},

I have this function I want after the success of the request to redirect I a spa way without page reloading to question single page I am using inertia js and jetstream my laravel router is below

Route::middleware(['auth:sanctum', 'verified'])->get('/question/{question}', 'App\Http\Controllers\QuestionController@show')->name('question-single');

Solution

  • Simply use the visit method on the inertia like shown below.

    this.$inertia.visit(route('question-single'), { method: 'get' });
    

    If you got everything correct from your code above remaining the redirection without your page reloading, then I guess the modification of your code will be the sample as folows;

    summitQuestion(){
    this.question_button = true
    axios.post('/api/question/ask', {
        'title' : this.question.title,
        'body' : this.question.body,
        'tags' : this.tags,
        'user_id' : this.$page.props.user.id
    }).then(response=>{
        this.question_button = false
        // console.log(response.data)
        this.$inertia.visit(route('question-single'), { method: 'get', data: response.data });
    
    
    }).catch(error=>{
        this.question_button = false
        console.log(error.response.data.errors)
    
        if(error.response.data.errors){
            this.title_errors = error.response.data.errors.title
            this.body_errors = error.response.data.errors.body
        }
    
    
    })
    

    },

    You can make reference to this by visiting The Official Inertiajs Website