I 'm using Laravel's middleware, and I'm trying to have an empty div if the user is not authenticated. Otherwise, I am trying to have a div that allows the user to create an account. I have tried the following:
#auth
<div class="form-group col-md-6"></div>
#else
<div class="form-group col-md-6">
<h6>Create Account</h6>
</div>
#endauth
Routes
Route::get('/checkout', 'CheckoutController#index')->name('checkout.index')->middleware('auth');
Route::get('/guestCheckout', 'CheckoutController#index')->name('guestCheckout.index');
CheckoutController.php
if (auth()->user() && request()->is('guestCheckout')) {
return redirect()->route('checkout.index');
}
The above does not work, and I have tried deleting the history, clearing caches, etc. but to no avail. Suggestions? Thank you!
trying to have an empty div if user is not authenticated...
Your blade setup is really close. Try this!
#auth
<!-- the user is authenticated -->
<div class="form-group col-md-6>
</div>
#endauth <!-- note the end auth -->
#guest
<!-- the user is NOT authenticated -->
<div class="form-group col-md-6>
<h6>Create Account</h6>
</div>
#endguest
You can read up more about this here
As for your controller, try using:
if( Auth::check() )
{
// The user is authenticated..
} else {
// The user is NOT authenticated..
}
There's out of the box way to do this!
#guest
<div class="form-group col-md-6>
</div>
#else
<div class="form-group col-md-6>
<h6>Create Account</h6>
</div>
#endguest
You don't need the middleware thingy if I were you.
You can Do it like this
<ul>
#guest
#if (Route::has('login'))
<li><a class="nav-link scrollto {{Request::url() === route('login') ? 'active' : ''}}" href="{{route('login')}}">Login</a></li>
#endif
#if (Route::has('register'))
<li><a class="nav-link scrollto {{Request::url() === route('register') ? 'active' : ''}}" href="{{route('register')}}">Register</a></li>
#endif
#else
<li class="dropdown"><span>{{ Auth::user()->name }}</span> <i class="bi bi-chevron-down"></i>
<ul>
<li>
{{ __('Logout') }}
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
#csrf
</form>
</li>
</ul>
</li>
#endguest
</ul>
The current (Laravel 8) way to do this is by using the Auth helper directives.
https://laravel.com/docs/8.x/blade#authentication-directives
#auth
This only shows for logged in users
#endauth
#guest
This only shows for guests
#endguest
Related
Please help me, i'm right now try to change Add to Cart Button to Go to Cart after click, how possible I can do that?, please help me, I have trying but didn't work, here my condition code in Controller:
if (Temporary::where('course_id','=', $request->IDCourse)->exists()) {
Session::flash('failed-session','Go to card');
}
else
{
Session::flash('failed-session','Add to cart');
}
and below my blade for looping product on cart box
#foreach($carts as $cart)
<ul class="list-group list-group-flush">
<li class="list-group-item bg-light">
<div class="row">
<div class="col">
<input type="text" name="IDCourse" value="{{ $cart->course_id }}">
<a href="{{ route('course.detail', $cart->course->id) }}" class="text-body">
<div class="d-flex">
#if($cart->course->image == '')
<img src="{{ asset('storage/courses/Default_Photo.png') }}" alt="" class="avatar-md square" />
#else
<img src="{{ asset('storage/courses/'.$cart->course->image) }}" alt="" class="avatar-md square" />
#endif
<div class="ms-3">
<h5 class="fw-bold mb-1">
{{ $cart->course->title }}
</h5>
<span class="fs-6 text-muted">
<span>{{ __('Oleh ') }}{{ $cart->course->user->name }}</span>
</span>
</div>
</div>
</a>
</div>
</div>
</li>
</ul>
<hr class="my-0">
#endforeach
And the last code to display the result from condition in controller
#if(Session::has('failed-session'))
<div class="alert alert-danger">
{{Session::get('failed-session')}}
</div>
#endif
I try dd($request->course_id) and displaying null. My planning is to check if customer have add to cart that item then the button change to Go to Cart. Please help me.Thank u so much for any help.
simply just check this item in your controller then initialize the variable and check if variable is empty or not , then you can change the action of the form or change the src of the button ,
for example : if user->cart->count() > 0
redirect to the page
you can add another button for continue shopping as well too .
When I try to http://domain.test/logout then showing "The GET method is not supported for this route. Supported methods: POST"
But normal logout with post method working perfectly. How can I change the /logout route Post to get.
in Jetstream, fortify
if you list your routes using php artisan route:list you'll see that logout route is defined for POST, so you just need to submit a form to this route in order to fire logout.
POST | logout | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController#destroy | web
Now if you wish to convert that function to respond to GET method, you'll need to change that route in /vendor/laravel/fortify/routes/route.php
I have not tested it in any way, but the method and routes are there.
If still anyone is looking for it you can try this.
In Laravel 8 get method doesn't support logout route you can try post method like below.
<form method="POST" action="{{ route('logout') }}">
#csrf
<div class="nav-item">
<a class="nav-link" href="{{ route('logout') }}" onclick="event.preventDefault();
this.closest('form').submit(); " role="button">
<i class="fas fa-sign-out-alt"></i>
{{ __('Log Out') }}
</a>
</div>
</form>
You can also try this by check in if it's login then it will show you logout otherwise it will show you login .
#if (auth()->id())
<form method="POST" action="{{ route('logout') }}">
#csrf
<div class="nav-item">
<a class="nav-link" href="{{ route('logout') }}" onclick="event.preventDefault();
this.closest('form').submit(); " role="button">
<i class="fas fa-sign-out-alt"></i>
{{ __('Log Out') }}
</a>
</div>
</form>
#else
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}" role="button">
<i class="fas fa-sign-in-alt"></i>
Login
</a>
</li>
#endif
For Anyone Facing This Problem in Laravel Jetstream use this Code
<form method="POST" action="{{ route('logout') }}">
#csrf
<x-jet-dropdown-link href="{{ route('logout') }}" onclick="event.preventDefault(); this.closest('form').submit();">
<i class="fa fa-sign-out"></i>{{ __('Logout') }}
</x-jet-dropdown-link>
</form>
You should put them in the base.blade.php file or the file where the logout form is supposed to be
I have created several pieces of my Laravel 7 application (I'm a newbie trying to learn). I then wanted to add authentication on top of that, so I did:
php artisan ui bootstrap --auth
npm install && npm run dev
After doing so, I have a "login" and a "register" link on the top left-corner of every page of my app. These links are being added by some automated method and are not changing the pages (views - *.blade.php files) that I had created before adding the authentication system.
I'd like to prevent these links from being added automatically. Then I can add my own links to my navigation bar. The problem is I can't figure out how they're being created or added to my views. Can someone help me?
NEVERMIND - I FIGURED IT OUT.
Appearently during the authentication system scaffolding my "layout.blade.php" was modified to include both the "login" and "register" links by the addition of this code just under the <body> tag:
<div class="flex-center position-ref full-height">
#if (Route::has('login'))
<div class="top-right links">
#auth
Home
#else
Login
#if (Route::has('register'))
Register
#endif
#endauth
</div>
#endif
</div>
All I needed to do is remove that code block. Hope this helps someone in the future.
The login links are created on a separate template file (views/layouts/app.blade.php). To remove them you need to customize this file.
This is the part where the header links are created:
<!-- Right Side Of Navbar -->
<ul class="navbar-nav ml-auto">
<!-- Authentication Links -->
#guest
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
#if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
#endif
#else
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
</div>
</li>
#endguest
</ul>
I need allow links in messages, when user send message. How I can do this?
#foreach($mess as $message)
<li data-mid="{{ $message->id }}">
<div class="row margin-left-none margin-right-none">
<div class="col-md-2 padding-right-none">
<div class="avatar-user text-center">
<img src="{{ $message->sender->avatar }}" alt="$message->sender->name">
</div>
</div>
<div class="col-md-10 padding-left-none">
<div class="user-name">
<span>{{ $message->sender->name }}
#if(Helper::getOnlineUser($message->sender->id))
<i data-toggle="tooltip" title="Онлайн" class="material-icons online">fiber_manual_record</i>
#else
<i data-toggle="tooltip" title="Оффлайн" class="material-icons offline">fiber_manual_record</i>
#endif
<span class="date float-right">{{ $message->created_at->formatLocalized('%H:%m') }}</span></span>
</div>
<div class="message">
{{ $message->body }}
</div>
</div>
</div>
</li>
#endforeach
This is view of messages.
How I can allow links?
Example: http://example.com in messages =>
http://example.com
I need {!! $message->body !!} ? Or How?
In controller:
$mess = Chat::conversations($conversation)->for($user)->getMessages($limit, $request->page);
I use this package: https://github.com/musonza/chat
There are built in PHP functions to handle this:
<div class="message">
{!! strip_tags($message->body, '<a>') !!}
</div>
Where the second argument is your whitelist.
This has the following advantages:
You're not messing with regex
You're still using blade {!!!!}
You're using well tested code dev'ed by the core team
You can do it by using following php function in your blade,
<?php
function getDataWithURL($text){
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "".$url[0]." ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
}
?>
You just need to add this code in your blade file and then you can check and replace the links in your text like this,
<div class="message">
<?php getDataWithURL($message->body);?>
</div>
I hope you will understand.
I use Hesto/multi-auth package. The username when I login success default redirect in customer.layout.auth, how can I to redirect in my blade, Example: welcome.blade.php, I can't use {{ Auth::user()->name }} in another blade, it error Trying to get property of non-object . How to fix it, please help me !
AppServiceProvider.php
public function register()
{
//
if ($this->app->environment() == 'local') {
$this->app->register('Hesto\MultiAuth\MultiAuthServiceProvider');
}
}
auth.blade.php
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="{{ url('/customer/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/customer/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
#endif
I had the same problem and I think I found the solution.
You are creating your routes on the web route file.
You need to create your routes inside user route file created by Hesto/multi-auth package.
I don't know if this is the right way of doing it but it worked for me.