How to trigger IntertiaUI modal from a Laravel controller?
To trigger an InertiaJS modal from a Laravel controller, you need to follow these steps:
- Create a modal component in InertiaJS:
First, create a new InertiaJS component for your modal. You can create a new file in the app/Modules/YourModule/Views/Components
directory, for example, app/Modules/YourModule/Views/Components/Modals/YourModal.vue
.
Here's an example of what the component could look like:
<template>
<div>
<button @click="showModal = true">Open Modal</button>
<transition enter-active-class="transition ease-out duration-100" leave-active-class="transition ease-in duration-150">
<div v-if="showModal" class="fixed z-10 inset-0 overflow-y-auto" aria-labelledby="modal-title" role="dialog" aria-modal="true">
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" aria-hidden="true"></div>
<span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">​</span>
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full">
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<!-- Your modal content goes here -->
</div>
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
<button @click="showModal = false" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:ml-3 sm:w-auto sm:text-sm">
Close
</button>
</div>
</div>
</div>
</div>
</transition>
</div>
</template>
<script>
export default {
data() {
return {
showModal: false,
};
},
};
</script>
- Create a route for the modal component:
Next, you need to create a new route for your modal component. You can create a new route file in the routes/web.php
file, for example:
Route::get('/modals/your-modal', 'YourController@yourModal')->name('your-modal');
- Create a controller method for the modal:
Create a new controller method in your Laravel controller to render the modal component. For example:
namespace App\Http\Controllers;
use Inertia\Inertia;
class YourController extends Controller
{
public function yourModal()
{
return Inertia::render('Components/Modals/YourModal');
}
}
- Trigger the modal from a Laravel route or action:
Finally, you can trigger the modal from a Laravel route or action by returning the yourModal
method from your controller. For example, in a store
method of a controller:
public function store(Request $request)
{
// Your validation and business logic here
return redirect()->route('your-route')->with('success', 'Your message');
}
public function yourRoute()
{
return Inertia::render('Components/Modals/YourModal');
}
In this example, when the store
method is called and the validation and business logic are successful, the user will be redirected to the yourRoute
method, which will render the modal component.
You can also trigger the modal from a route by defining a route with a closure and returning the yourModal
method:
Route::get('/your-route', function () {
return Inertia::render('Components/Modals/YourModal');
});
Now, when the user navigates to the /your-route
URL, the modal component will be rendered.