In a project I'm currently working on, there are many tables. There is specific table for user images.
I want to display this image in the navbar.
The navbar is in a file Navbar.blade.php. It has no controller or anything.
What would be a good practice to fetch the image data? Where should I SELECT from db? In the Navbar file itself? Or I should do something else?
Edit: This project is a mess. So there is no relationship between the users and their images.
Should I create 2 models for Users and for Images (Since each has its own table), and make a relation ship between them? Then maybe use auth()->user->image?
@Snapey ty! will try. Btw before you answer, I already started creating a Model for the images table only and made a relationship between the User and the Photo. What do you think is a better idea? A relationship or an accessor? And why?
For an accessor in my case it would be something like:
public function getImage()
{
return DB::table('images')->where('id', '=', Auth::user->id)->value('image_path');
}
// then use it with User->getImage();
versus using a relationship and then get the image path like so:
@Ligonsker The only performance consideration would be if you listed all users with their images, in which case you would want to eager load the images to avoid n+1
If you load the image in the model then a database hit would occur, but if this is just one user then there is no difference in the number of queries.