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

NielsNumbers's avatar

How to return a success message with form helper?

On my vue page I have a notification box. I want that it pops up if the form was submitted sucessfully with a sucess message:

<template>
<div>
<NotificationBox :show="feedbackShow" :label="feedbackLabel" :desc="feedbackDesc" @close="feedbackShow=false" ></NotificationBox>
...
</div>
</template>

<script setup>
...

const feedbackShow = ref(false);
const feedbackLabel = ref('');
const feedbackDesc = ref('');

const form = useForm({
  email: '',
  name: '',
  password: '',
  role: '',
  notification: true,
});

const submit = () => {
  form.post('/dashboard/users/admins', {
    onStart: () => {
      feedbackShow.value = false;
    },
    onSuccess: () => {
      form.name = '';
      form.email = '';
      form.password = '';

      feedbackLabel.value = 'User sucessfully Added!';
      feedbackDesc.value = 'Email notifification has been send';
      feedbackShow.value = true;
    },
  });
}
</script>

In my controller, I have this as my create and store endpoint:

  public function create(Request $request): Response
    {
        $roles = Role::toBase()->where('id', '>=', Auth::user()->role_id)->orderBy('created_at')->select('name', 'id')->get();
        return Inertia::render('Dashboard/Users/Admins/Create', compact('roles'));
    }

    public function store(StoreAdminRequest $request): RedirectResponse
    {
        Admin::create([
            'password' => $request->input('password'),
            'email' => $request->input('email'),
            'role_id' => $request->input('role'),
            'name' => $request->input('name'),
        ]);

        // @todo notification missing..

        return back()->with('status', 'User ' . $request->input('name') . ' has been created. User can login with email ' . $request->input('email') . ' and with password ' . $request->input('password'));
    }

Now I don't understand how am I supposed to access the info status? I would like to update my feedbackDesc.value according to that status that I return in store method of controller.

Also, I noticed that sometimes I get a 409 response causing interia to reload the entire page, and removing my notification. I can not really reproduce it, it just happens sometimes. Any idea? Am I doing something wrong?

0 likes
2 replies
NielsNumbers's avatar
NielsNumbers
OP
Best Answer
Level 10

Okay I found out, I need to add session value to InteriaRequestsMiddleware:

public function share(Request $request): array
    {
        $isAdmin = Auth::user() instanceof Admin;
        //Log::debug(['session' => $request->session()]);
        return array_merge(parent::share($request), [
            'csrf' => csrf_token(),
            //'flash' => ['success' => fn () => $request->session()->get('success')],
            'flash' => [
                'notification' => session('notification')
            ],
            'auth' => self::share_auth(Auth::user()),
        ]);
    }

And the I can access it using usePage():

const submit = () => {
  form.post('/dashboard/users/admins', {
    onStart: () => {
      feedbackShow.value = false;
    },
    onSuccess: () => {
      form.name = '';
      form.email = '';
      form.password = '';

      console.log(usePage());
      console.log(usePage().props);

      feedbackLabel.value = 'User sucessfully Added!';
      feedbackDesc.value = usePage().props.value.flash.notification;
      feedbackShow.value = true;
    },
  });
}
1 like

Please or to participate in this conversation.