Search code examples
laravelimagestoragelaravel-8

How can I solve an image add issue on Laravel 8?


I have an issue with my Laravel8 project.

In my project, a user can post an add with an image. The image is well saved in the database but it's not displaying. And on my Console, I do have a 404 error message.

404 error message

Here is my code in my migration file :

    <?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddColumnImageUrlToOffersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('offers', function (Blueprint $table) {
            //store images
            $table->string('image_url');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('offers', function (Blueprint $table) {
            //drop the columns
            $table->dropColumn('image_url');
        });
    }
}

Here is my code in my layout file :

            <div class="w-1/2 rounded shadow overflow-hidden">
            <img class="object-cover w-full h-96" src="{{asset($offer->image_url)}}" alt="" srcset="">
        </div>

Thanks for your help


Solution

  • Check your storage folder permissions
    if you are on localhost try 777
    if you are on server you can try 755

    Create symbolic link

    php artisan storage:link
    

    Specify a public disk, example

    $path = $request->file('file')->store(
        'offer_images/'.$request->user()->id, 'public'
    );
    

    Retrieve file

    $url = Storage::url('file.jpg');
    

    Since you are getting 404 Not Found try retrieving file with domain

    $url = env('APP_URL').Storage::url('file.jpg');