I am new to Laravel and I am confused with these snippets that I got automatically when I installed the laravel UI by using the command compose require laravel/ui I am not quite getting what it's trying to do.
#if (Route::has('login'))
<div class="top-right links">
#auth
Home
#else
Login
#if (Route::has('register'))
Register
#endif
#endauth
</div>
#endif
In English, it's saying that if you have laravel/ui installed and Auth::routes() in the web.php routes file then if you also logged in (authenticated) show the "Home" link. Otherwise, if you are not logged in, show the login link and then if there is a route for 'register' then display the register link. You can verify your current routes from the command line by issuing: php artisan route:list.
The code snippet is just to show the Menu Link based on whether you are logged in or not
#if (Route::has('login'))
<div class="top-right links">
#auth
Home
#else
Login
#if (Route::has('register'))
Register
#endif
#endauth
</div>
#endif
In the above code snippet #if (Route::has('login')) It just checks if you have any route that is registered with name login if yes
#auth this one checking if you are already logged in then it will show 'Home' link else it will show 'Login' link
#if (Route::has('register')) this one again checking if you have any route defined with name register then it will also show 'Register' link as well
Related
Im working on a e-commerce web-app using laravel. I have a section called shop by brand with the pictures of the brands and when i click on the picture i want it to show me the products of that brand.
This is the blade part of my code where i call the route:
<div class="brand-slides owl-carousel owl-theme">
<div class="brand-item">
<img src="assets/img/brand/shopbybrand1.jpg" alt="image">
</div>
</div>
This is my controller:
public function searchVendor()
{
$searchproduct="Fanola";
$product=product::Where('vendor','LIKE',"%$searchproduct%")->get();
return view('home.userpage',compact('product'));
}
At the moment its just a simple code that will work with the "Fanola" brand only. I did it this way just to test if it works.
And this is my route in my web.php:
Route::get('/searchbybrand', [HomeController::class, 'searchVendor']);
The error it shows me when i serve it is "Route [searchbybrand] not defined."
Ive tried clearing the route cache and it still doesnt work. I asked chat gpt about this and it gave me different ideas like defining the route like this:
Route::get('search-by-brand', 'HomeController#searchVendor')->name('searchbybrand');
When i do it like this it shows a different error: "Target class [HomeController] does not exist."
I also tried composer dump-autoload, php artisan config:clear and php artisan cache:clear as per its suggestions but none of them worked.
this is because your route doesn't have a name you can fix it like this
Route::get('/searchbybrand', [HomeController::class, 'searchVendor'])->name('searchbybrand');
or instead of that in your blade you can use url instead of route
<div class="brand-slides owl-carousel owl-theme">
<div class="brand-item">
<img src="assets/img/brand/shopbybrand1.jpg" alt="image">
</div>
</div>
and for your second problem which says Target class [HomeController] does not exist. add this in top of your web.php
use App\Http\Controllers\HomeController;
also at the end run
php artisan optimize
I have a nicely working basic install of Laravel JetStream and Spatie's laravel-permission in Laravel 8.
I can assign a role to the user during registration via
$user->assignRole('visitor');
return $user;
and can restrict the available menu items on the user's dashboard through the permissions I have assigned to the role in my seeder filés run method:
Permission::create(['name' => 'access profile']);
Permission::create(['name' => 'access logout']);
$visitor = Role::create(['name' => 'visitor']);
$visitor->givePermissionTo('access profile');
and through the can directive in the view, like:
#can('access profile')
<!-- Account Management -->
<div class="block px-4 py-2 text-xs text-gray-400">
{{ __('Manage Account') }}
</div>
<x-jet-dropdown-link href="{{ route('profile.show') }}">
{{ __('Profile') }}
</x-jet-dropdown-link>
#endcan
So by that, I can hide the menu item as per role but unfortunately, I can still access the functionality directly, by knowing the exact URL.
I guess I have to write a middleware to restrict access to certain functions, but how exactly?
What is the proper and accepted way to handle this problem in this stack?
Thanks!
Armand
So everything seems fine BUT (!)
How is it possible to forbid direct access to the hidden items? I guess in this case routes are controlled by sanctum, while roles and permissions are by Spatie's package.
Is it possible to link the two?
Thanks!
Did you try this? It seems like they added exactly the same for Spatie. Nevertheless I think you need to add a gate permission check like
abort_if(Gate::denies('permission'), Response::HTTP_FORBIDDEN, '403 Forbidden');
on every action
I would see if you can utilize laravel's built in can middleware. Then you might be able to update your route definitions. Something like
Route::get('/profile', 'ProfileController#index')->middleware('can:access profile');
I haven't done this with the package you're using, but I think it should work if the other built-in functionality like blade #can work.
I have multiple guards in my system
Admin
User
Admin and User both can add the Organizations details but they have different template layout. What i'm doing now i create views of each guard like this
views
user
organizations
index.blade.php
_form.blade.php
create.blade.php
edit.blade.php
admin
organizations
index.blade.php
_form.blade.php
create.blade.php
edit.blade.php
Now i want to create one views which can be used by multiple guards with different layouts
views
organizations
index.blade.php
_form.blade.php
create.blade.php
edit.blade.php
From the Laravel Blade Documentation:
If needed, you may specify the authentication guard that should be checked when using the #auth and #guest directives:
#auth('admin')
// The user is authenticated...
#endauth
#guest('admin')
// The user is not authenticated...
#endguest
In laravel view file:
#if(Session::has('success'))
<p class="alert alert-info">{{ Session::get('success') }}</p>
#endif
above code not working, its getting display on view file as it is..
But I am using simple php instead of that:
<?php if (Session::has('success')){ ?>
<p class="alert alert-info"><?php echo Session::get('success'); ?></p>
<?php } ?>
Its working properly.. then why this is happning
php code is working properly but simple laravel code is not working in laravel view/blade file.. its getting printed on view page as it is..
please clarify this..
I think your view file extension wrong please correct you view file like index.blade.php
I'm having this weird issue with my app, it can't persist logged in user session using the Auth facade. Example, in my header view I put logged in user data as :
#if (Auth::check())
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<img src="{{route('getphoto', Auth::user()->image)}}" alt="" />
{{ Auth::user()->name }}
<span class="caret"></span>
</button>
</div>
#else
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Not Logged In
</button>
</div>
#endif
At the first time after login the logged in user data in the header like photo and name will be displayed correctly, but after I refresh or moving to another page they disappear and indicate that the user is not logged in. I don't know what I did wrong, all I did with user login related function is just changing the email to username. How do I solve this ? I'm using Laravel 5.4
You need to set correct permissions for the storage directory:
chmod -R 755 storage
After installing Laravel, you may need to configure some permissions. Directories within the storage and the bootstrap/cache directories should be writable by your web server.
https://laravel.com/docs/5.4/installation