Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 865
Blessing Mwale Avatar asked Jun 20 '26 21:06

Blessing Mwale


1 Answers

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

like image 83
Destiny Brotobor Avatar answered Jun 22 '26 23:06

Destiny Brotobor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!