Mihai
Mihai

Reputation: 26784

Inertia redirect issue in Laravel with Vue

I have a modal in vue where I call this function

const  createChat = async (id) =>{
    try {
        const response = await axios.post('/pdf-chat/create-chat', {
            name: name.value,
            pdfId: id,

        });
        dialogVisible.value = false;
        console.log(response)
    

    } catch (error) {
            console.error('Error sending message:', error);

    }
}

The controller function called,the route exists

public function createChat( Request $request)
    {
        $chat = new Chat();
        $chat->name = $request->name;
        $chat->save();


        $pdf = Upload::find($request->pdfId);


        return Inertia::location(route('pdf-chat', ['chat' => $chat->hash, 'pdf' => $pdf->hash]));
}

I get a redirect 302 enter image description here

And the next request is the redirect url with 200 code but I stay on the same page,nothing in the browser`s url changes. If a copy the url and paste it in the browser it works fine. enter image description here

Any thoughts? Laravel 10,Vue 3 ,Inertia 1.*

Upvotes: 0

Views: 1882

Answers (2)

Othmane Nemli
Othmane Nemli

Reputation: 1193

Don't use (it's only for External redirects):

Inertia::location()

use:

to_route('pdf-chat', ['chat' => $chat->hash, 'pdf' => $pdf->hash])

Update:

I see now, because you are using axios you should use inertiajs router inertiajs.com/manual-visits

Upvotes: 3

waterloomatt
waterloomatt

Reputation: 3742

Instead of using Axios to make the request, use Inertia's router.visit or one of their helpers like router.get. These will intercept the request and know how to handle the response.

See https://inertiajs.com/manual-visits

router.visit('/pdf-chat/create-chat', {
  method: 'get',
  data: {
    name: name.value,
    pdfId: id
  }
})

Upvotes: 1

Related Questions