Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

laravel - 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}', 'AppHttpControllersQuestionController@show')->name('question-single');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are using Inertia, you are supposed to create a form with v-model linked to fields. Add to that a button of type submit that call your method (see below the method example).

<form @submit.prevent="submitQuestion">
    <input type="text" v-model="form.title">
    <button type="submit"></button>
</form>
<script>
    export default {
        data() {
          return {
            form: this.$inertia.form({
              title: this.question.title,
              body: this.question.body,
              tags: this.tags,
              user_id: this.$page.props.user.id,
            }, {
              bag: 'default',
            }),
          }
        },
        methods: {
          summitQuestion: function() {
            this.form.post('/api/question/ask');
          },
        }
    };
</script>

The redirection can be done directly on your controller method.

class QuestionController extends Controller
{
    public function create(Request $request) {
        // Create your question
        return redirect()->route('your.route');
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...