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

0

After logging in, I need to show a message (open a message screen - a modal or a separate view -) on every page it visits. The message will only stop being displayed when you press "Do not show message again". This information will be saved in a table so that the message is no longer displayed.

The solution I did was create an ajax and check whether the table it clicked or not on "does not show message". Is there another way to do this with Laravel?

    
asked by anonymous 18.10.2018 / 21:10

3 answers

0

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

The calculation of the parameter to be sent to the views can be calculated in a middleware and included in the ApplicationController constructor (or controllers constructors that make sense).

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

Solve the problem in a clean way and save one request per page.

    
18.10.2018 / 21:27
0

Without using some form of storage I find it difficult, but I do not think it's necessary a ajax to check this every view loaded. You can access the user attribute (that is being used in the user table) to check whether you clicked or not. And use ajax only to change those values.

Example:

app.blade.php

//...

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

//...
    
18.10.2018 / 21:29
0

Including subviews

The Blade @include directive allows you to include a Blade view from within another view . All variables that are available for parent view are made available for viewing.

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

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

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

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

Of course, if you attempt a view that does not exist, 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'd 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 view that exists for a particular view array, you can use the following directive: @includeFirst

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

Source: Official laravel documentation

I think the most appropriate approach would be:

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

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

    
18.10.2018 / 21:31