How to open a message screen in all the views the user logs in? + Laravel

Asked

Viewed 149 times

0

After logging in, I need to show a message (open a message screen -a modal or in a separate view-) on every page he visits. The message will only stop being shown when it is pressed "No longer show the message". This information will be saved in a table so that this message will no longer be shown.

The solution I made was to create an ajax and check whether or not in the table he clicked on "do not show the message". There’s another way to do this with Laravel?

3 answers

1

Since the action will only be triggered when you enter the screen, there is no need for the request to be made through ajax. You can return a parameter in your controller indicating that it has been accepted in the message.

Parameter calculation to be sent to views can be calculated in a middleware and included in the Applicationcontroller constructor (or controller constructors that make sense).

In the layout you render any partial where you would include the component that displays the message and the control not to view more, according to the parameter calculated in the above mentioned middleware.

Solves the problem in a clean way and saves one request per page.

0

Without using any form of storage I find difficult, but I believe it is not necessary a ajax to check it every view loaded. You can access the user attribute (which is being used in the user table) to check whether or not you clicked. And use the ajax only to change these values.

Example:

app.blade.php

//...

@if (Auth::user()->naoMostrar == 0)
    //Modal e ações(js, ..) que o acionem
@endif

//...

0

Including sub-views

The directive @include Blade allows you to include a view of Blade inside another view. All variables that are available for viewing in Parent will be made available for viewing.

<div>
    @include('shared.errors')

    <form>
        <!-- Form Contents -->
    </form>
</div>

Even if the included view inherits all data available in the parent view, you can also pass an extra data array to the included view:

@include('view.name', ['some' => 'data'])

Of course, if you try a view that doesn’t exist, the Laravel will throw an error. If you would like to include a view that may or may not be present, you should use the directive: @includeIf

@includeIf('view.name', ['some' => 'data'])

If you would like to include, depending on a particular boolean condition, you can use the directive: @includeWhen

@includeWhen($boolean, 'view.name', ['some' => 'data'])

To include the first display that exists of a particular array of views, you can use the directive: @includeFirst

@includeFirst(['custom.admin', 'admin'], ['some' => 'data'])

Source: Official documentation of the Laravel

I believe the most appropriate approach would be:

@includeWhen(Auth::user()->mostrarMensagem, 'view.name');

That way you will only include this view if the variable is true

Browser other questions tagged

You are not signed in. Login or sign up in order to post.