Notification not working on Multiple guards - laravel

I have 2 guards. One 'user' and the other 'admin'.
I have stored notification in database as admin. But guard('admin') is not working.
Here is my code in blade file:
<span class="badge badge-light">{{count(auth()->guard('admin')->user()->unreadNotifications())}}</span>
But i am only getting '1' as a result. I have 6 records in my table. Also my forech loop is not working either with guard. Here is the code:
<ul class="dropdown-menu">
#foreach(auth()->guard('admin')->user()->notifications() as $notifications)
<li>{{$notifications->type}}</li>
#endforeach
</ul>
Thanks a bunch

Try using notifications and unreadNotifications without ()
auth()->guard('admin')->user()->notifications
//
auth()->guard('admin')->user()->unreadNotifications
When using () on relationships you are always retrieving a Eloquent or Query Builder instance

Related

Zizaco Entrust Laravel make a mysql request for each #permission in the page

I am using Laravel 5.2 with Zicaco Entrust for the management of permissions and roles.
I have a menu for the user and I use the laravel blade system in order to show the menu with #permission for each menu section because I am only showing the menu section if the user has the permission to see it.
#permission(['upload'])
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i>DB Feed <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
#permission(['file-upload'])
<li>file upload</li>
#endpermission
#permission(['***-upload'])
<li>*** upload</li>
#endpermission
#permission(['***-upload'])
<li>*** upload</li>
#endpermission
#permission(['***-upload'])
<li>*** upload</li>
#endpermission
</ul>
</li>
</ul>
#endpermission
And it continues like this as I have something like 50 different #permission in my menu section.
Now when I look the different mysql access, I can see that it does 50 times the same request to the DB:
select `roles`.*, `role_user`.`user_id` as `pivot_user_id`, `role_user`.`role_id` as `pivot_role_id` from `roles` inner join `role_user` on `roles`.`id` = `role_user`.`role_id` where `role_user`.`user_id` = '1'
select `permissions`.*, `permission_role`.`role_id` as `pivot_role_id`, `permission_role`.`permission_id` as `pivot_permission_id` from `permissions` inner join `permission_role` on `permissions`.`id` = `permission_role`.`permission_id` where `permission_role`.`role_id` = '1'
How can I change this behavior so that it does only 1 single set of request?
Thanks.
Ok, so it seems that the Entrust addon to Laravel is not developped anymore. I could find a solution though through the cache.php file in the config folder and add the following line:
'ttl' => 30 //In minutes

How to prevent loops in laravel global variable

I've created file GlobalVariable.php inside app\Composers
public function compose($view)
{
$categories = \App\Models\Category::all();
$view->with('globCategory', $categories);
}
then register to AppServiceProvider the code view()->composer('*', GlobalVariable::class);
I use global $globCategory for creating dynamic navbar
<ul class="nav nav-tabs border-0 flex-column flex-lg-row">
#foreach ($globCategory as $item)
<li class="nav-item">
{{$item->name}}
</li>
#endforeach
</ul>
the only problem here when I see laravel debuggar it show repetition of categories query.
here is the result
How to avoid this looping query? is there correct way?
The way you're registering your view composer (using '*' instead of a particular view name), it's going to call the compose method for every single rendered view + subview.
What you can do is instead of this:
view()->composer('*', GlobalVariable::class);
Have this:
\View::share('globCategory', \App\Models\Category::all());
This will globally share your categories (within views), and run the query only once.
View composers, as described from the laravel documentation, bind data to a view every time it is rendered. They clean our code by getting fetching data once and passing it to the view.
While it is possible to get the data in every controller method and pass it to the single view, this approach may be undesirable.
Replace the view name with an asterisk wildcard.

Laravel 5.6 dynamic pages from a database best practice

I am using/learning Laravel 5.6 and wondered if this is the best approach in trying to accomplish dynamic pages from a database.
The approach I have taken works but I feel it could be improved especially with having to retrieve the pages for the navigation bar with every request.
I have a route in web.php
Route::get('/', 'PageController#index')->name('index');
Route::get('/{page}', 'PageController#show');
I then have my page controller with index and show functions.
public function index()
{
$pages = Page::all();
$posts = Post::latest('created_at')->paginate(2);
return view('index', compact('posts','pages'));
}
public function show($uri)
{
$pages = Page::all();
$page = Page::where('uri', $uri)->first();
return view('templates.page', compact('page','pages'));
}
Now in my header.blade.php I display the list of pages like this:
#foreach($pages as $page)
<li class="nav-item">
<a class="nav-link" href="/{{ $page->uri }}">{{ $page->title }}</a>
</li>
#endforeach
Now my problem is with all the other controllers I have to get the page's information from the database everytime which seems inefficient.
Any advice would be appreciated. Thank you in advance.
You can share your pages data with every view automatically by adding View::share('key', 'value');in the boot method of a service provider. Alternatively you can create a view composer.
one layout blade.
yield title by
<title>#yield('page-title')</title>
then on view,
//process title using php from DB
then display using
#section('page-title')
{{$page_title}}
#endsection

Does Laravel Auth::user()->name and Auth::user()->email do two querys

I have a blade view for logged in users
To display my users name and email I do this
<p>{{ Auth::user()->name }}</p><br>
<p>{{ Auth::user()->email }}</p>
But what happens in the backround?
Does Laravel do two querys? One to fetch the users name and one to fetch the email?
I just wonder if I should contionue to do like the above code or just fetch alla data in need in the controller with one query.
It will do only one query. To remind my self of this I usually stick the user in the route and pass to the blade template:
$user = Auth:user();
return view('yourview', ['user' => $user]);
Then in blade you can use:
<p>{{$user->id}}</p>
<p>{{$user->name}}</p>
<p>{{$user->email}}</p>
Either way is fine i just find this way cleaner.
If you want to see all the queries your app runs when you visit a page check out Laravel Debugbar
Laravel Debugbar

Laravel 4 view composers not working

I saw this question. I have a similar problem, but it ain't working. Laravel 4 public functions.
I have a view composer that includes some codes in base layout. Here's my composer:
View::composer(array('common.menu_addition','common.base_errors','common.view_special'), function($view)
{
if(Auth::check()) {
$roles = Auth::user()->type;
if ($roles == '5') {
$view->with('roles', $roles);
} else {
return Redirect::to('news/index');
}
}
});
When I'm not logged in, it works perfectly. But one of my files of view composer goes like this:
<div class="pull-right">
#if (Auth::check())
#if ($roles == 5)
<ul class="nav">
<li>
<a href="{{ URL::to('admin/dash') }}">
<i class="icon-eye-open">
</i>
<strong>
Admin Dashboard
</strong>
</a>
</li>
</ul>
#endif
#endif
</div>
When I login, it won't display any site. It just says Undefined variable: roles in that view composer file.
I don't think it's possible to return a redirect like that, in your view composer. And even if you could it's a bad place to put that logic. You should create a filter http://four.laravel.com/docs/routing and redirect away if the user doesn't have the proper authentication level.
It looks like you're not defining $roles beforehand and not passing $roles into your views.
When you're making checks against the $roles in views you need to make sure that variable actually exists. Other possibility is to change the check to something like
#if (isset($roles) && $roles == 5)
to ignore the error but that gets really messy in the long run.
So make sure to initialise view variables beforehand in the view composer and everywhere else. Laravel tries to be much more stricter about non-initialised variables than PHP in common.

Resources