Ikong
Ikong

Reputation: 2570

Laravel 5 triggering modal in view conditionally

I want to check if user is login, if so then display some content, else i want to click a url or call modal.

@if(Auth::check())
    .............if user registered display here
@else
   I want to click the link below automatically....OR Call a view
   <a class="signin_link" href="{{ action('Auth\AuthController@login') }}" rel="get:Loginform"><i class="fa fa-user" style="font-size:20px"></i></a>
@endif

Upvotes: 0

Views: 876

Answers (2)

noodles_ftw
noodles_ftw

Reputation: 1313

While you can do it in the view, you shouldn't. Instead, use the auth middleware (see https://github.com/laravel/laravel/blob/master/app/Http/Middleware/Authenticate.php).

Here's an example:

Route::group(['middleware' => 'auth'], function() {
    Route::get('/this/route/needs/a/logged/in/user', 'PageController@account');
});

If there's no user logged in the visitor is redirected to the login page (which you can set in the middleware).

Upvotes: 0

Fabio Antunes
Fabio Antunes

Reputation: 22862

This will redirect your user to the page you want:

@if(Auth::check())
    .............if user registered display here
@else
   //Redirect user to the link
   <script>window.location = "{{ action('Auth\AuthController@login') }}";</script>
@endif

And this would open your modal, if you are using bootstrap for example:

@if(Auth::check())
    .............if user registered display here
@else
   //Opening a bootstrap modal
   <script>$('#myModal').modal('show')</script>
@endif

Note: I assume that you already have your modal html somewhere on your page

Upvotes: 1

Related Questions