How to Display an Image From a Database in Laravel











up vote
0
down vote

favorite












I have an admin template that I registered into an app from a purchase. The template is very well set up, and parts of it are written in Laravel. I'm able to register new users in my modification, but the user profile image is having problems. When a newly registered user is enacted, the file name is stored in the respective database. However, the image is not able to be displayed. I can even see the image name in the Google Chrome web tools when inspecting the app code. This image is being saved, it's just not displaying on the webpage (at least not on the sidebar).



@if (auth()->user()->profile->pic == null)
<img src="{{asset('storage/uploads/users/no_avatar.jpg')}}" alt="user-img" class="img-circle">
@else
<img src="{{ auth()->user()->profile->pic }}" alt="user-img" class="img-circle">
@endif


If there is something that I'm doing wrong, it must be a basic Laravel issue. I don't know how, but the image must be able to be taken from the database and shown in the view that I pull up or navigate to. If someone could tell me how to properly include the profile pic on the frontend view like any other Laravel app, it would help me a lot. Thank you.










share|improve this question
























  • What exactly are you storing in pic? Is the image stored in the public folder?
    – adam
    Nov 10 at 21:33















up vote
0
down vote

favorite












I have an admin template that I registered into an app from a purchase. The template is very well set up, and parts of it are written in Laravel. I'm able to register new users in my modification, but the user profile image is having problems. When a newly registered user is enacted, the file name is stored in the respective database. However, the image is not able to be displayed. I can even see the image name in the Google Chrome web tools when inspecting the app code. This image is being saved, it's just not displaying on the webpage (at least not on the sidebar).



@if (auth()->user()->profile->pic == null)
<img src="{{asset('storage/uploads/users/no_avatar.jpg')}}" alt="user-img" class="img-circle">
@else
<img src="{{ auth()->user()->profile->pic }}" alt="user-img" class="img-circle">
@endif


If there is something that I'm doing wrong, it must be a basic Laravel issue. I don't know how, but the image must be able to be taken from the database and shown in the view that I pull up or navigate to. If someone could tell me how to properly include the profile pic on the frontend view like any other Laravel app, it would help me a lot. Thank you.










share|improve this question
























  • What exactly are you storing in pic? Is the image stored in the public folder?
    – adam
    Nov 10 at 21:33













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an admin template that I registered into an app from a purchase. The template is very well set up, and parts of it are written in Laravel. I'm able to register new users in my modification, but the user profile image is having problems. When a newly registered user is enacted, the file name is stored in the respective database. However, the image is not able to be displayed. I can even see the image name in the Google Chrome web tools when inspecting the app code. This image is being saved, it's just not displaying on the webpage (at least not on the sidebar).



@if (auth()->user()->profile->pic == null)
<img src="{{asset('storage/uploads/users/no_avatar.jpg')}}" alt="user-img" class="img-circle">
@else
<img src="{{ auth()->user()->profile->pic }}" alt="user-img" class="img-circle">
@endif


If there is something that I'm doing wrong, it must be a basic Laravel issue. I don't know how, but the image must be able to be taken from the database and shown in the view that I pull up or navigate to. If someone could tell me how to properly include the profile pic on the frontend view like any other Laravel app, it would help me a lot. Thank you.










share|improve this question















I have an admin template that I registered into an app from a purchase. The template is very well set up, and parts of it are written in Laravel. I'm able to register new users in my modification, but the user profile image is having problems. When a newly registered user is enacted, the file name is stored in the respective database. However, the image is not able to be displayed. I can even see the image name in the Google Chrome web tools when inspecting the app code. This image is being saved, it's just not displaying on the webpage (at least not on the sidebar).



@if (auth()->user()->profile->pic == null)
<img src="{{asset('storage/uploads/users/no_avatar.jpg')}}" alt="user-img" class="img-circle">
@else
<img src="{{ auth()->user()->profile->pic }}" alt="user-img" class="img-circle">
@endif


If there is something that I'm doing wrong, it must be a basic Laravel issue. I don't know how, but the image must be able to be taken from the database and shown in the view that I pull up or navigate to. If someone could tell me how to properly include the profile pic on the frontend view like any other Laravel app, it would help me a lot. Thank you.







php database laravel laravel-blade avatar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:57









Karl Hill

1,7491736




1,7491736










asked Nov 10 at 21:29









diggX2414

13




13












  • What exactly are you storing in pic? Is the image stored in the public folder?
    – adam
    Nov 10 at 21:33


















  • What exactly are you storing in pic? Is the image stored in the public folder?
    – adam
    Nov 10 at 21:33
















What exactly are you storing in pic? Is the image stored in the public folder?
– adam
Nov 10 at 21:33




What exactly are you storing in pic? Is the image stored in the public folder?
– adam
Nov 10 at 21:33












1 Answer
1






active

oldest

votes

















up vote
0
down vote













In order to access your storage path you can use storage_path helper function instead of asset so your code should be:



storage_path('uploads/users/no_avatar.jpg');


and for the user that has uploaded picture, if you store the relative path to the picture stored in the public folder then you can use



{{ url(auth()->user()->profile->pic) }}


if the value of auth()->user()->profile->pic is img/profile.jpg then you should have img folder containing profile.jpg within your project public directory.






share|improve this answer





















  • Okay, but what if the data I want is only within a separate database connected to the laravel app? Couldn't it be obtained like the user's name?
    – diggX2414
    Nov 10 at 23:29










  • I thought there was a way for the app to display info from a database interaction. It just doesn't seem right to store all users in the storage or public directories.
    – diggX2414
    Nov 11 at 2:12












  • the picture has to exists physically on some server in order for you to display it. I don't get your point of the app being able to display info from a database interaction. You can use Amazon S3 service or similar in order to keep the pictures somewhere, but they have to exist. And what you keep in the DB is usually a path to the image, not the image itself.
    – nakov
    Nov 11 at 8:56










  • I understand that, but I'm not sure how to select the image. If the path is what is stored in the database, how do I put that exact path in the database?
    – diggX2414
    Nov 11 at 15:18










  • laravel.com/docs/5.7/filesystem#file-uploads so from here you store the $path to the database.
    – nakov
    Nov 11 at 20:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243585%2fhow-to-display-an-image-from-a-database-in-laravel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













In order to access your storage path you can use storage_path helper function instead of asset so your code should be:



storage_path('uploads/users/no_avatar.jpg');


and for the user that has uploaded picture, if you store the relative path to the picture stored in the public folder then you can use



{{ url(auth()->user()->profile->pic) }}


if the value of auth()->user()->profile->pic is img/profile.jpg then you should have img folder containing profile.jpg within your project public directory.






share|improve this answer





















  • Okay, but what if the data I want is only within a separate database connected to the laravel app? Couldn't it be obtained like the user's name?
    – diggX2414
    Nov 10 at 23:29










  • I thought there was a way for the app to display info from a database interaction. It just doesn't seem right to store all users in the storage or public directories.
    – diggX2414
    Nov 11 at 2:12












  • the picture has to exists physically on some server in order for you to display it. I don't get your point of the app being able to display info from a database interaction. You can use Amazon S3 service or similar in order to keep the pictures somewhere, but they have to exist. And what you keep in the DB is usually a path to the image, not the image itself.
    – nakov
    Nov 11 at 8:56










  • I understand that, but I'm not sure how to select the image. If the path is what is stored in the database, how do I put that exact path in the database?
    – diggX2414
    Nov 11 at 15:18










  • laravel.com/docs/5.7/filesystem#file-uploads so from here you store the $path to the database.
    – nakov
    Nov 11 at 20:25















up vote
0
down vote













In order to access your storage path you can use storage_path helper function instead of asset so your code should be:



storage_path('uploads/users/no_avatar.jpg');


and for the user that has uploaded picture, if you store the relative path to the picture stored in the public folder then you can use



{{ url(auth()->user()->profile->pic) }}


if the value of auth()->user()->profile->pic is img/profile.jpg then you should have img folder containing profile.jpg within your project public directory.






share|improve this answer





















  • Okay, but what if the data I want is only within a separate database connected to the laravel app? Couldn't it be obtained like the user's name?
    – diggX2414
    Nov 10 at 23:29










  • I thought there was a way for the app to display info from a database interaction. It just doesn't seem right to store all users in the storage or public directories.
    – diggX2414
    Nov 11 at 2:12












  • the picture has to exists physically on some server in order for you to display it. I don't get your point of the app being able to display info from a database interaction. You can use Amazon S3 service or similar in order to keep the pictures somewhere, but they have to exist. And what you keep in the DB is usually a path to the image, not the image itself.
    – nakov
    Nov 11 at 8:56










  • I understand that, but I'm not sure how to select the image. If the path is what is stored in the database, how do I put that exact path in the database?
    – diggX2414
    Nov 11 at 15:18










  • laravel.com/docs/5.7/filesystem#file-uploads so from here you store the $path to the database.
    – nakov
    Nov 11 at 20:25













up vote
0
down vote










up vote
0
down vote









In order to access your storage path you can use storage_path helper function instead of asset so your code should be:



storage_path('uploads/users/no_avatar.jpg');


and for the user that has uploaded picture, if you store the relative path to the picture stored in the public folder then you can use



{{ url(auth()->user()->profile->pic) }}


if the value of auth()->user()->profile->pic is img/profile.jpg then you should have img folder containing profile.jpg within your project public directory.






share|improve this answer












In order to access your storage path you can use storage_path helper function instead of asset so your code should be:



storage_path('uploads/users/no_avatar.jpg');


and for the user that has uploaded picture, if you store the relative path to the picture stored in the public folder then you can use



{{ url(auth()->user()->profile->pic) }}


if the value of auth()->user()->profile->pic is img/profile.jpg then you should have img folder containing profile.jpg within your project public directory.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 21:40









nakov

1,44388




1,44388












  • Okay, but what if the data I want is only within a separate database connected to the laravel app? Couldn't it be obtained like the user's name?
    – diggX2414
    Nov 10 at 23:29










  • I thought there was a way for the app to display info from a database interaction. It just doesn't seem right to store all users in the storage or public directories.
    – diggX2414
    Nov 11 at 2:12












  • the picture has to exists physically on some server in order for you to display it. I don't get your point of the app being able to display info from a database interaction. You can use Amazon S3 service or similar in order to keep the pictures somewhere, but they have to exist. And what you keep in the DB is usually a path to the image, not the image itself.
    – nakov
    Nov 11 at 8:56










  • I understand that, but I'm not sure how to select the image. If the path is what is stored in the database, how do I put that exact path in the database?
    – diggX2414
    Nov 11 at 15:18










  • laravel.com/docs/5.7/filesystem#file-uploads so from here you store the $path to the database.
    – nakov
    Nov 11 at 20:25


















  • Okay, but what if the data I want is only within a separate database connected to the laravel app? Couldn't it be obtained like the user's name?
    – diggX2414
    Nov 10 at 23:29










  • I thought there was a way for the app to display info from a database interaction. It just doesn't seem right to store all users in the storage or public directories.
    – diggX2414
    Nov 11 at 2:12












  • the picture has to exists physically on some server in order for you to display it. I don't get your point of the app being able to display info from a database interaction. You can use Amazon S3 service or similar in order to keep the pictures somewhere, but they have to exist. And what you keep in the DB is usually a path to the image, not the image itself.
    – nakov
    Nov 11 at 8:56










  • I understand that, but I'm not sure how to select the image. If the path is what is stored in the database, how do I put that exact path in the database?
    – diggX2414
    Nov 11 at 15:18










  • laravel.com/docs/5.7/filesystem#file-uploads so from here you store the $path to the database.
    – nakov
    Nov 11 at 20:25
















Okay, but what if the data I want is only within a separate database connected to the laravel app? Couldn't it be obtained like the user's name?
– diggX2414
Nov 10 at 23:29




Okay, but what if the data I want is only within a separate database connected to the laravel app? Couldn't it be obtained like the user's name?
– diggX2414
Nov 10 at 23:29












I thought there was a way for the app to display info from a database interaction. It just doesn't seem right to store all users in the storage or public directories.
– diggX2414
Nov 11 at 2:12






I thought there was a way for the app to display info from a database interaction. It just doesn't seem right to store all users in the storage or public directories.
– diggX2414
Nov 11 at 2:12














the picture has to exists physically on some server in order for you to display it. I don't get your point of the app being able to display info from a database interaction. You can use Amazon S3 service or similar in order to keep the pictures somewhere, but they have to exist. And what you keep in the DB is usually a path to the image, not the image itself.
– nakov
Nov 11 at 8:56




the picture has to exists physically on some server in order for you to display it. I don't get your point of the app being able to display info from a database interaction. You can use Amazon S3 service or similar in order to keep the pictures somewhere, but they have to exist. And what you keep in the DB is usually a path to the image, not the image itself.
– nakov
Nov 11 at 8:56












I understand that, but I'm not sure how to select the image. If the path is what is stored in the database, how do I put that exact path in the database?
– diggX2414
Nov 11 at 15:18




I understand that, but I'm not sure how to select the image. If the path is what is stored in the database, how do I put that exact path in the database?
– diggX2414
Nov 11 at 15:18












laravel.com/docs/5.7/filesystem#file-uploads so from here you store the $path to the database.
– nakov
Nov 11 at 20:25




laravel.com/docs/5.7/filesystem#file-uploads so from here you store the $path to the database.
– nakov
Nov 11 at 20:25


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243585%2fhow-to-display-an-image-from-a-database-in-laravel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python