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

madprabh's avatar

Inertia doesn't update page with latest data

Hey Folks,

When I update a model in the database and then redirect user back to model list page the data is not updated

return to_route('forms.fields',['survey'=> $question->survey_id]); 

I am using Inertia and Vue.

Any idea where I am going wrong?

0 likes
9 replies
LaryAI's avatar
Level 58

When using Inertia.js with Vue, it's important to ensure that the data being displayed is always the latest from the server. If you're experiencing issues where the page doesn't update with the latest data after a redirect, here are a few steps you can take to resolve the issue:

  1. Ensure Fresh Data Fetching: Make sure that when you redirect back to the list page, the server is fetching the latest data from the database. This typically involves ensuring that your controller method is querying the database for the most recent data.

  2. Use Inertia's Redirect: Instead of using to_route, you should use Inertia's redirect helper to ensure that the page is re-rendered with fresh data. This can be done using the Inertia::render method in your controller.

  3. Check Your Inertia Response: Ensure that the data you are passing to the Inertia response is indeed the latest. You can log the data being sent to the client to verify this.

Here's an example of how you might structure your controller method to ensure fresh data is sent to the client:

use Inertia\Inertia;
use App\Models\Question;

public function update(Request $request, $id)
{
    // Update the question
    $question = Question::findOrFail($id);
    $question->update($request->all());

    // Fetch the latest data for the list page
    $questions = Question::where('survey_id', $question->survey_id)->get();

    // Redirect back to the list page with fresh data
    return Inertia::render('Forms/Fields', [
        'questions' => $questions,
    ]);
}
  1. Client-Side Considerations: Ensure that your Vue component is correctly set up to receive and display the data passed from the server. If you're using props, make sure they are correctly defined and used in your component.

  2. Cache Busting: If you have any caching mechanisms in place, ensure they are not serving stale data. This includes browser caching or any server-side caching.

By following these steps, you should be able to ensure that your Inertia.js application always displays the most up-to-date data after a redirect.

oakydev's avatar

Can you confirm that the data is actually updated in the database?

madprabh's avatar

@oakydev Yes, I confirmed that. The DB does get updated but for some reason the redirect doesn't load the updated data. When I manually refresh the browser it does show me the updated data.

oakydev's avatar

@madprabh How do you submit your data? Do you use the useForm function and the appropriate http method when submitting?

madprabh's avatar

@oakydev Yes I am using the use form function here is the code

In my vue template

import { useForm, usePage } from '@inertiajs/vue3'; 

function storeQuestion() {
    form.put(route('questions.update', {'question': usePage().props.edit_question_id}), {
      preserveScroll: true,

    })
  }

const form = useForm({
    title: '',
    id:'',
    
});
form.id = usePage().props.edit_question_id;
form.title = usePage().props.question.title;

In my controller

public function update(Request $request, Question $question, QuestionService $questionService)
    {
        //
        $data=$request->all();
        $validated = $request->validate([
            'title' => 'required|string',

        ]);  
        $question->title=$data['title'];
        //return $question;
        try {
            if($questionService->updateQuestion($question))
            {
                return to_route('forms.fields',['survey'=> $question->survey_id]); 
            }
        }
        catch (\Exception $e)
        {
            return $e->getMessage();
        }
    }
madprabh's avatar

I think I know what the problem might be

const question_list = ref(usePage().props.survey_questions.pages[0].question_ids);

So in my code I am expecting question_list to change when there is a new question added to the "question_ids" array. But I think vue cannot keep track of single element within the array.

Is my analysis correct here?

What do I do to get this to work?

madprabh's avatar

Ok I fixed it by using this for now. Not sure if this is the best practice but it is what it is for now

watch(() => usePage().props.survey_questions.pages[0].question_ids, (newQuestionIds) => {
	  question_list.value = newQuestionIds
	})
gych's avatar

Did you already try by adding preserveState: false to form.put?

    form.put(route('questions.update', {'question': usePage().props.edit_question_id}), {
      preserveScroll: true,
	  preserveState: false 
    })
2 likes
madprabh's avatar

@gych Let me give this a shot. I hadn't tried this. Thanks.

Please or to participate in this conversation.