0

I have a form where you can submit images. It checks it it is an an image and it saves it into the database. My issue is how do i view it? I don't know the url for the image.

The ImageController:

public function upload(Request $request)
    {
        
        $validatedData = $request->validate([
         'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
        ]);
        $name = $request->file('image')->getClientOriginalName();
        $path = $request->file('image')->store('public/images');
        $save = new Image;
        $save->name = $name;
        $save->path = $path;
        $save->save();
        return redirect('/')->with('mssg', 'Image Has been uploaded');
    }

Form:

@extends('layouts.app')
@section('content')
    <div class="wrapper adoption-details">
            <h1>Adoption for {{ $animal->name }}</h1>
            <img src="{{ asset('/image') }}" />
            <p>Name: {{ $animal->name }} </br>
               DoB: {{ $animal->dob }} </br>
               Availability: {{ $animal->available }} </br>
               Description: {{ $animal->description }} </br></p>
            
            <form name="image-upload" id="image-upload" method="post" action="{{url('upload')}}" enctype="multipart/form-data">
                 @csrf
        <div>
          <label for="choose">Please Select Image To Upload:</label>
          <input type="file" id="image" name="image" class="@error('image') is-invalid @enderror form-control">
          @error('image')
              <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
          @enderror
        </div>
        <button type="submit" Submit="btn btn-primary">Submit</button>
      </form>
     </div>
    <a href="/animals" class="back">Back to all requests</a>
@endsection

What would be the <img src="{{ asset('/image') }}" /> be? The path of the image is storage/app/public/images/.

3
  • Read this section of the Laravel documents: laravel.com/docs/8.x/filesystem#file-uploads and use this approach. As you can see in config/filesystems.php there are several disks defined by default. The one you are looking for is public. Retrieving the file would be {{ Storage::disk('public')->url($image->path); directly into the img->src.The reason why public exists is because it's a bad idea to upload files directly into the public folder. Don't forget to create the symbolic link on both test and production php artisan storage:link Commented Apr 11, 2021 at 15:16
  • @DimitriMostrey, I don't have a config/filesystem.php? Commented Apr 11, 2021 at 15:50
  • On a Laravel setup? It's one of the core conig files. It automatically comes with a fresh laravel installation. Very strange indeed. This file shows your 'disks' or places you can put and retrieve files. This could also be Amazon S3 or similar. Commented Apr 12, 2021 at 1:34

1 Answer 1

2

You can configure your disk in config/filesystem.php. Something like this:

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

In .env file select default disk. You can configure many disks. Then save the path in database in column image (like a varchar) and add this column to SQL query. Remeber to add command php artisan storage:link then in blade use <img src="{{ Storage::url($animal->image) }}" />

Laravel documentation is very helpful. Read this Laravel Docs - Filesystem. I think you will find answer.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the correct answer, except you may have to define the correct disk. The default is local and not public, hence <img src="{{ Storage::disk('public')->url($animal->image) }}" />, unless of course, @Alia sets the default to public.
@Isek, I don't have a config/filesystem.php?
@Alia which laravel version are you using? Laravel 7 or 8?
@HassaanAli, 8.36.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.