avatarMark Caggiano

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3036

Abstract

6d">But this data will available just in the controller, and in the main component….</p><h1 id="f0f5">What If I Want To Access Data Every Request ?</h1><p id="f7b8">If we want to access data at every request, like session data or generic data, without write code in every controller; we have to add a piece of code in a middleware, that should already be there, called <b>HandleInertiaRequests.php.</b></p><p id="30d3">Inertia.JS has a special function called <b>share, </b>which lets you share data between components, locally in a controller/page, or globally if you use a middleware or a similar concept.</p><p id="7399">So let’s make Flash Messages available globally, adding this code inside <b>HandleInertiaRequests.php</b>:</p><div id="8102"><pre>/**

  • Defines the props that are shared by default.

  • @see https://inertiajs.com/shared-data

  • @param \Illuminate\Http\Request $request

  • @return<span class="hljs-built_in"> array </span> */<span class="hljs-keyword"> public</span> function share(Request request):<span class="hljs-built_in"> array </span>{ <span class="hljs-built_in"> return </span>array_merge(parent::share(request), [ 'flash' => [ 'message' => fn () => request-&gt;session()-&gt;get('message') ], ]); }</pre></div><p id="70e2">Now we can access this data by simply doing this in our components:</p><div id="6991"><pre><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">"page.props.flash.message"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flex items-center"</span> <span class="hljs-attr">role</span>=<span class="hljs-string">"alert"</span>></span>

     <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span></span><span class="hljs-template-variable">{{ <span class="hljs-name">$page.props.flash.message</span> }}</span><span class="language-xml"><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    

    <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"></<span class="hljs-name">template</span>></span></span></pre></div><p id="d910">To make it more easier… i also created a Vue component called FlashMessages, which you can personalize as you wish. Here it is the code:</p><div id="84b6"><pre><template> <div v-if="$page.props.flash.message" class="flex items-center rounded bg-blue-<span class="hljs-number">500</span> text-white text-sm font-bold px-<span class="hljs-number">4</span> py-<span class="hljs-number">3</span>" role="alert"> <svg class="fill-current w-<span class="hljs-number">4</span> h-<span class="hljs-number">4</span> mr-<span class="hljs-number">2</span>" xmlns="http://www.w3.org/<span class="hljs-number">2000</span>/svg" viewBox="<span class="hljs-number">0 0 20 20</span>"> <

Options

path d="M12.<span class="hljs-number">432 0c1</span>.<span class="hljs-number">34 0 2.01</span>.<span class="hljs-number">912 2.01</span> <span class="hljs-number">1.957 0</span> <span class="hljs-number">1.305-1</span>.<span class="hljs-number">164 2.512</span>-<span class="hljs-number">2.679 2</span>.<span class="hljs-number">512</span>-<span class="hljs-number">1.269 0</span>-<span class="hljs-number">2</span>.<span class="hljs-number">009</span>-.<span class="hljs-number">75-1.974</span>-<span class="hljs-number">1</span>.<span class="hljs-number">99C9.789</span> <span class="hljs-number">1.436 10</span>.<span class="hljs-number">67</span> <span class="hljs-number">0 12.432</span> <span class="hljs-number">0</span>zM8.<span class="hljs-number">309</span> <span class="hljs-number">20</span>c-<span class="hljs-number">1.058 0</span>-<span class="hljs-number">1</span>.<span class="hljs-number">833</span>-.<span class="hljs-number">652</span>-<span class="hljs-number">1.093-3</span>.<span class="hljs-number">524l1.214</span>-<span class="hljs-number">5</span>.<span class="hljs-number">092</span>c.<span class="hljs-number">211</span>-.<span class="hljs-number">814.246-1</span>.<span class="hljs-number">141 0-1.141</span>-.<span class="hljs-number">317 0-1</span>.<span class="hljs-number">689.562</span>-<span class="hljs-number">2.502 1</span>.<span class="hljs-number">117</span>l-.<span class="hljs-number">528</span>-.<span class="hljs-number">88c2.572</span>-<span class="hljs-number">2.186 5</span>.<span class="hljs-number">531</span>-<span class="hljs-number">3.467 6</span>.<span class="hljs-number">801</span>-<span class="hljs-number">3.467 1</span>.<span class="hljs-number">057 0 1</span>.<span class="hljs-number">233 1.273</span>.<span class="hljs-number">705</span> <span class="hljs-number">3</span>.<span class="hljs-number">23</span>l-<span class="hljs-number">1.391 5</span>.<span class="hljs-number">352</span>c-.<span class="hljs-number">246.945</span>-.<span class="hljs-number">141 1.271</span>.<span class="hljs-number">106 1.271</span>.<span class="hljs-number">317 0 1</span>.<span class="hljs-number">357</span>-.<span class="hljs-number">392</span> <span class="hljs-number">2.379-1</span>.<span class="hljs-number">207</span>l.<span class="hljs-number">6.814C12</span>.<span class="hljs-number">098 19.02</span> <span class="hljs-number">9.365 20</span> <span class="hljs-number">8</span>.<span class="hljs-number">309</span> <span class="hljs-number">20</span>z"/> </svg>

    &lt;p&gt;{{ $page.props.flash.message }}&lt;/p&gt;
&lt;/div&gt;

</template>

<script> import { defineComponent } from 'vue'

export default defineComponent({})

</script></pre></div><p id="02e1">You can find a lot of premade alerts using TailwindCSS, i just copied one from their website.</p><p id="c1dd">So thank you very much for reading, please clap if you found this article to be usefull.</p><p id="b05a">Thanks,</p><p id="2d3a">Mark.</p></article></body>

How To Display Flash Messages With Laravel 9 + Jetstream + Vue Inertia.JS

Laravel Jetstream can be confusing, so i decided to write articles to make it easy to develop using this stack.

Laravel Jetstream + Vue Inertia.JS is a very powerfull stack you can use to develop all kind of applications really fast. The only problem is that first time can be really confusing.

Today i was developing an app and i came across the necessity to display FlashMessages using Inertia.JS.

First things first. You can pass data to Inertia.JS pages directly from controller to the page, like this:

<?php

namespace App\Http\Controllers;

use App\Models\PaymentType;
use Illuminate\Http\Request;
use Inertia\Inertia;

class DashboardController extends Controller
{
    /**
     * Instantiate a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['auth:sanctum', 'verified']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Inertia\Response
     */
    public function index()
    {
        $data = [1, 2, 3, 4, 5]

        return Inertia::render('Dashboard', $data);
    }
}

But this data will available just in the controller, and in the main component….

What If I Want To Access Data Every Request ?

If we want to access data at every request, like session data or generic data, without write code in every controller; we have to add a piece of code in a middleware, that should already be there, called HandleInertiaRequests.php.

Inertia.JS has a special function called share, which lets you share data between components, locally in a controller/page, or globally if you use a middleware or a similar concept.

So let’s make Flash Messages available globally, adding this code inside HandleInertiaRequests.php:

/**
 * Defines the props that are shared by default.
 *
 * @see https://inertiajs.com/shared-data
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'flash' => [
            'message' => fn () => $request->session()->get('message')
        ],
    ]);
}

Now we can access this data by simply doing this in our components:

<template>
    <div v-if="$page.props.flash.message" class="flex items-center" role="alert">

        <p>{{ $page.props.flash.message }}</p>
    </div>
</template>

To make it more easier… i also created a Vue component called FlashMessages, which you can personalize as you wish. Here it is the code:

<template>
    <div v-if="$page.props.flash.message" class="flex items-center rounded bg-blue-500 text-white text-sm font-bold px-4 py-3" role="alert">
        <svg class="fill-current w-4 h-4 mr-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
            <path d="M12.432 0c1.34 0 2.01.912 2.01 1.957 0 1.305-1.164 2.512-2.679 2.512-1.269 0-2.009-.75-1.974-1.99C9.789 1.436 10.67 0 12.432 0zM8.309 20c-1.058 0-1.833-.652-1.093-3.524l1.214-5.092c.211-.814.246-1.141 0-1.141-.317 0-1.689.562-2.502 1.117l-.528-.88c2.572-2.186 5.531-3.467 6.801-3.467 1.057 0 1.233 1.273.705 3.23l-1.391 5.352c-.246.945-.141 1.271.106 1.271.317 0 1.357-.392 2.379-1.207l.6.814C12.098 19.02 9.365 20 8.309 20z"/>
        </svg>

        <p>{{ $page.props.flash.message }}</p>
    </div>
</template>

<script>
    import { defineComponent } from 'vue'

    export default defineComponent({})
</script>

You can find a lot of premade alerts using TailwindCSS, i just copied one from their website.

So thank you very much for reading, please clap if you found this article to be usefull.

Thanks,

Mark.

PHP
Laravel
Inertia Js
Vuejs
Php 8
Recommended from ReadMedium