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

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

Related

how to display data based on specific id in laravel?

I am new to laravel. I want to send data as id of existing data. Id comes from products.blade I send via href tag as shown below to gallery page. I have tried to find a way through other sites but it still doesn't work
<a class="btn btn-success" href="/dashboard/galleries/{{ $product->id }}"><i class="ri-image-add-line text-white"></i></a>
then i create a route like this
Route::resource('/dashboard/galleries', DashboardGalleryController::class)->middleware('admin')->shallow();
in the controller gallery, I made like this
public function index($id)
{
$gallery = Gallery::where('products_id', $id)->get();
return view('dashboard.galleries.index', compact('gallery'));
}
then inside the gallery page, I want to display a table to add images based on that id.
dashboard/galleries/index.blade.php
<h1>{{ $gallery->id }}</h1>
should i add data inside foreach or outside?
A restful resource controller sets up some default routes for you and even names them.
and ur case the logic should be inside show() function not index()
check this issue it will help u same issue solved here

how to hide id from URL laravel 7?

i have this url :
http://127.0.0.1:8000/deliverer/4
i want it like this
http://127.0.0.1:8000/deliverer/
this is my function :
public function show($id)
{
// $userhash->hashids->encode($id);
$user=User::find($id);
return view('deliverer.profile')->with('user',$user);
}
and this is my route
Route::get('deliverer/{id}', 'deliverer\DelivererController#show')->name('profile');
and this in view
<a href="http://127.0.0.1:8000/deliverer/{{ Auth::user()->id }}" >
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
Assuming, that what you're trying to accomplish is to view the current authenticated user's profile, then you should follow the steps below:
First you have to modify the route and remove the {id} from the URL, like this:
Route::get('deliverer', 'deliverer\DelivererController#show')->name('profile');
Then, inside the controller you have to remove the $id param from the show() method and change the method to get the id from the authenticated user.
public function show($id)
{
// $userhash->hashids->encode($id);
$user = \Auth::user();
return view('deliverer.profile')->with('user',$user);
}
And of course, you have to remove the Auth::user()->id() from the view route, and perhaps use the named route instead of hardcoding it, like so:
<a href="{{ route('profile') }}">
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
I'm assuming you're just trying to hide id's so users can guess the next number or try to pull up records they shouldn't.
Have you looked into UUID's? Example here: (https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be) That might be a solution.
Also, if you are worried that someone might tamper with URL to pull records, you should look into securing up your models. Do a check to see if the user should have access to that particular record. Many ways to accomplish that.
You can use token or configure a middleware, or as you did you can hash or crypt the id and make the verification after the call
The url will be like that :
http://127.0.0.1:8000/deliverer/?id=aze45a8sd54q

Laravel 5.7 Pagination Customization

$users = Student::paginate(2);
I was trying to paginate some data with paginate function() but am want to show only data no links
You can customize the way pagination is displayed, first step:
php artisan vendor:publish --tag=laravel-pagination
This command publishes the pagination views under the resources/views/vendor directory.
Here you can modify files as you want, the bootstrap-4.blade.php file is the default view for pagination.
You can find more info in the laravel official documentation
Using the $students->links() is optional if yo have a custom blade file showing pagination. However, if you do not need detailed pagination, you can use simplePaginate
$students = Student::simplePaginate(2);
And then in blade file just loop through it :
#foreach($students as $student)
Show student details
#endforeach
If you want to show just next or prev page links then you can use $students->links(), otherwise do not use links at all.
it's simple you must create a function to display your students in your template, add this code in your controller :
public function index()
{
$students = Student::paginate(10);
return view('student.index', ['students ' => $students ]);
}
And on the bottom of your template for example views\student\index.blade.php add :
<div class="row">
<div class="col-md-12">
{{ $students->links() }}
</div>
</div>
if you want display additional pagination information use the following methods: :
Paginator Instance Methods

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

Notification not working on Multiple guards

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

Resources