Laravel Displaying image from database
When you retrieve a file path stored in your database and bind it directly to an <img> tag, you are essentially telling the browser to load a relative URL based on that path. If $post->image contains uploads/photo.jpg, the resulting HTML renders as src="uploads/photo.jpg". The browser then attempts to resolve this relative to the current page URL. Unless your application is running at the root of a domain and the file sits directly in a public subdirectory matching that structure, the browser will fail to find the resource.
The core issue here is not about saving the image twice or changing how you write the tag; it is about ensuring the server can serve the file at the exact URL your HTML requests. Laravel's asset() helper and its built-in storage system are designed to handle this resolution automatically, but only if the file actually exists in a public directory accessible via HTTP.
Understanding Storage Paths vs. Public URLs
In Laravel, there is a distinct difference between where a file lives on the filesystem and how it should be accessed by a browser. The public directory acts as the root for all web-accessible resources. When you upload an image using Form Requests or a controller action, you typically save the file to storage/app/public/uploads. However, this path is not directly accessible via HTTP unless you create a symbolic link to your public folder.
You must run the following command in your terminal:
php artisan storage:link
Without this symlink, Laravel cannot serve files from storage/app/public, and any attempt to display an image with that path will result in a 404 error. Once linked, you can access the file at http://yoursite.com/storage/app/public/uploads/photo.jpg.
Using the Asset Helper
The most robust way to display an image stored on disk is by using Laravel's asset() helper. This function takes a path relative to your public folder and returns a full URL that works across different environments, including local development and production servers.
If your database stores the filename as uploads/photo.jpg, you should construct the source like this:
<img src="{{ asset('storage/app/' . $post->image) }}" alt="Post Image">
Alternatively, if you have already established the public symlink and your storage path is configured correctly in .env (often set to storage/app/public), you might simply point the asset helper directly to the subdirectory:
<img src="{{ asset('storage/' . $post->image) }}" alt="Post Image">
The key takeaway is that the path passed to asset() must correspond to a location inside your public root after the symlink has been created.
Storing URLs vs. File Paths
Another common approach in modern web development is to store the full URL of the image directly in your database rather than just the filename. This decouples your application from the specific directory structure on the server. If you use an external service like AWS S3, Cloudinary, or a local CDN, you would store something like https://example.com/s3-bucket/photo.jpg in the image column.
In this scenario, you can pass the value directly into the <img> tag without any path manipulation:
<img src="{{ $post->image }}" alt="Post Image">
This method eliminates the need for storage:link entirely, provided the URL is valid and publicly accessible. However, it introduces a dependency on external services or requires managing your own static file hosting infrastructure. If you rely solely on local storage, the symlink approach remains the standard best practice.
Avoiding Common Pitfalls
It is important to note that using HTML::image() in older Laravel versions often yielded similar results because it ultimately generated the same <img> tag with a relative path if not configured otherwise. The underlying issue persists regardless of the helper used: the browser needs a valid, absolute URL or a correct relative path that resolves to an existing file on the server.
If you are migrating from older Laravel versions where public_path() was used manually, ensure you are not double-prefixing your paths. For instance, if $post->image already includes storage/app/public/, passing it to asset() will result in a path like /asset/storage/app/public/storage/app/public/uploads/photo.jpg, which is incorrect. Always strip the base storage directory name from the database column if you intend to use the asset() helper with the standard symlink setup.
For more advanced scenarios involving dynamic image resizing or secure CDN integration, Laravel provides packages and native features that can further streamline asset management. If you need guidance on integrating third-party services or optimizing your media pipeline further, resources like https://laravelcompany.com offer detailed documentation on extending these capabilities within the ecosystem.