2
votes

Hi I'm totally freaked out of Laravel filesystem, I'm searching around the web for three days and I can't find away to show images in view from "storage/app/public" folder or storing images in another folder outside "storage/app/public". I'm using Homestead on windows with Laravel 5.3. This is the "store controller" that uploads the images and saves the path in to the database.

    class ClassName extends Controller
{
public function store(Request $request)
        {
    $today = Carbon::today();  //Today
            $year = $today->year;                                         // year int(2016)
            $month = $today->month;                                        // month int(11)
            $day = $today->day;                                          // day int(5)

            //make year/month/day directory if not available then upload images to these directories sent from the form
            $path = $request->file('ProductImage')->store(public_path().'/'.$year.'/'.$month.'/'.$day);
            if ($request->file('ProductImageOne')) {
                $pathone = $request->file('ProductImageOne')->store('public'.'/'.$year.'/'.$month.'/'.$day);
            }
            else{
                $pathone = NULL;
            }

            if ($request->file('ProductImageTwo')) {
                $pathtwo = $request->file('ProductImageTwo')->store('public'.'/'.$year.'/'.$month.'/'.$day);
            }
            else{
                $pathtwo = NULL;
            }


            //store data's sent by the form
            $product = new Product;
            $product->ProductImage = $path;
            $product->ProductImageOne = $pathone;
            $product->ProductImageTwo = $pathtwo;
            $product->save();

            Session::flash('flash_message', 'Your Image is uploaded.');
            return redirect('/products/create');
    }
}

So far I don't have any Issue, the image is uploaded to the:

\storage\app\public\2016\11\8\image-name.png

But when I'm trying to show the image in the view file it won't show up.This is my controller method that sends the path to view:

public function edit($id)
    {
        //Get product from $id to edit
        $product = Product::findOrFail($id);       

        //send the data above and load the edit product view (edit form)
        return view('pages.edit_product')->with('product', $product);
    }

And this is my view code where I want to show the image:

<img src="{{ asset($product->ProductImage) }}" width="45">

Returns this:

<img src="http://laravel.dev:8000/public/2016/11/6/0b7e4e3dbf6907fa1b858fa5cd0bf3ce.jpeg" width="45">

http://laravel.dev:8000/public/2016/11/6/0b7e4e3dbf6907fa1b858fa5cd0bf3ce.jpeg

And when I want to directly go to this link to the image it says:

Sorry, the page you are looking for could not be found.

1/1 NotFoundHttpException in RouteCollection.php line 161:

And I even did the "symbolic link" through "php artisan storage:link" which I know a little about it. it makes a blank ".storage" file in the public folder. That's my first project with Laravel and I really loved it until I came to this issue uploading image and showing it to viewer and now I'm disappointed. Please Help

1
Did you create a symlink from your /public folder to your /storage/app/public folder? - Frnak
Yes I did and there's a blank ,store file that can't be edited inside the "laravel/public/" folder. - user2682025

1 Answers

1
votes

You need to remove public from the image url so that you have the following:

"http://laravel.dev:8000/2016/11/6/0b7e4e3dbf6907fa1b858fa5cd0bf3ce.jpeg"

So change store('public'.'/'.$year.'/'.$month.'/'.$day); to store($year.'/'.$month.'/'.$day);