How to access storage file using database query in LARAVEL

laravel db::query
laravel get file from storage
laravel raw query
laravel db::select
laravel raw query with parameters
laravel multiple databases
laravel database tutorial
laravel db::select count

I am working with laravel project and facing a problem. I created a database model called "Product" which have a field called "product_image" and its data type is string.

public function up()
    {
        Schema::create('Products', function (Blueprint $table) {
            $table->string('product_image')->nullable();
            $table->timestamps();
        });
    }

By using controller,I did enable uploading product image and product model capable to get that files active name with its extension,even I am able to find that uploaded file on storage/app/example.jpg.

$file = $request->file('product_image');
        $filename = 'product_image-' . time() . '.' . $file->getClientOriginalExtension();
        $path = $file->storeAs('product-image', $filename);
        $product->product_image = $path ;

But when I am running a @foreach loop on my blade template and trying to fetch image over this.

@foreach ($products as $p)
<tr>
<td>{{$p->product_title}}</td>
<td><img src="/storage/app/product-image/{{$p->product_image}}" alt="" style="height:80px; width:80px"/></td>
@endforeach

I am founding nothing and image field is still showing without original image :( enter image description here

I find out the problem but do not know the solution and lost in the dark. The actual problem is in, <img src="/storage/app/product-image/{{$p->product_image}}" alt="" style="height:80px; width:80px"/>

because its not founding the url for that image.

My question is, how we can provide URL for that particular image using for loop which will be able to locate that image from storage/app/example directory. Thanks in advance.


Have you been tried using artisan command for creating the symbolic link to the public files as describe here.

run php artisan storage:link

In your config/filesystems.php change into something like this:

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/product_image',
            'visibility' => 'public',
        ],
   ],

Then access your file like this in your blade template:

<img src="{{ asset('product_image/'. $p->product_image) }}" alt="" style="height:80px; width:80px"/>

Laravel 5, How do I display an image stored in the storage folder in blade? As Mayank Majithya notes, a symbolic link is one common way to access storage/app/public/ files from the public/ directory, and in fact the method the Laravel docs say you should use.


Are you able to access the image in your browser like - http://urdomain.com/storage/app/product-image/example.jpg ?

In your question you have mentioned storage/app/example.jpg but while accessing in your template you are referring it as storage/app/product-image/example.jpg. The path would be the problem?

Your loop looks like OK. IMO.

How to Connect a Database to Laravel, How can upload image in laravel and store in database and folder? Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.


Also, if you are using Vagrant or Laradock you should run your command php artisan storage:link inside your workspace, otherwise it won't work and will result to 404 when accessing it within your browser.

File Storage - Laravel, The filesystem configuration file is located at config/filesystems.php . To create the symbolic link, you may use the storage:link Artisan command: The get method may be used to retrieve the contents of a file. returned by the putFile method so you can store the path, including the generated file name, in your database. In this tutorial, i will show you how to access and display of storage folder image or file using route in laravel 5 application. we will create one route for access storage path for display image in admin side. So we can also give auth middleware to access only logged in user.


Basic Database Usage - Laravel, Errors & Logging · Events · Filesystem / Cloud Storage · Hashing · Helpers · Localization Laravel makes connecting with databases and running queries extremely simple. The database configuration file is config/database.php . When using multiple connections, you may access them via the DB::connection method: laravel-model-json-storage ⚠️ This package has been abandonned ⚠️ Do not hesitate to contact me if you want to make it evolve and to maintain it. Storing your models in a json file rather than in database (single or few lines recording) can be a good option.


Database: Query Builder - Laravel, You may use the table method on the DB facade to begin a query. You may access each column's value by accessing the column as a property of the object: All most developer and client want to store images or files secure on his server. so anyone can not access directly using url. that's the reason laravel introduce storage folder file upload. Almost using storage folder for uploading files and images on his laravel app.


Database: Getting Started - Laravel, Running Raw SQL Queries; Listening For Query Events; Database Transactions In this file you may define all of your database connections, as well as specify which After creating a new SQLite database using a command such as touch When using multiple connections, you may access each connection via the  Let’s edit the environment configuration file to set up the database details. Open the .env file using your command line editor of choice. Here, we will be using nano: nano .env Look for the database credentials section. The following variables need your attention: DB_HOST: your managed MySQL server host.