Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Aminium's avatar

fix reloading page in laravel reactjs inertiajs

hi i create thi sore post method using laravel reactjs inertiajs but when i submit data the page reload and i don't want this because i want create spa this is store method in PostController

public function store(Request $request)
    {
        $validatedData = $request->validate([
            'title' => ['required', 'string'],
            'description' => ['required', 'string', 'max:3000'],
            'status' => ['required', 'integer'],
            'category_id' => ['required', 'integer'],
            'tags' => ['required', 'array'],
            'tags.*' => ['required', 'string']
        ]);

        $post = Post::create($validatedData);
        $post->tags()->sync($validatedData['tags']);
        return redirect()->route('posts.index');  
    }
0 likes
2 replies
Aminium's avatar

and this is my web route

Route::post('/posts', [PostController::class, 'store'])->name('posts.store');
Aminium's avatar

and this is my handleSubmit function in jsk

const handleFormSubmit = async (data,e) => {
      e.preventDefault();
      try {
        const response = await Inertia.post(route('posts.store'), data);
        if (response && response.redirectTo) {
          console.log(data);
          Inertia.visit(response.redirectTo);
        }
      } catch (error) {
        console.error(error);
        console.log(data);
      }
    };

Please or to participate in this conversation.