Cannot get images from laravel 5 repository

Hey, so I'm trying to put my images in the slide shows that I have on my index. images are saved on storage/app , and then the rest of the path I retrieved from my database.

I managed to get the correct path in the <img src> , and the title will be displayed as expected, but the image will not.

I send arrays from the controller and then look at it in my view:

Controller:

 public function index() { $projects = Project::all()->take(4); return view('index' , compact('projects')); } 

Pointer view

 @foreach($projects as $project) {{$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix()}} <li><img src="{{$storagePath.$project->fetchMedia->first()->public_name}}" title=" {{$project->name}}"></li> @endforeach 

HTML output is the right way: "/home/vagrant/project/storage/app/projects/Domaa6.jpg"

but it does not display the image.

I already tried using the HTML :: image helper, but was unsuccessful.

Is there a way to make it work as it is, or do I need to create a new controller to manage my images?

fetchMedia is a function of my model that returns my media.

 public function fetchMedia() { return $this->hasMany('App\Media'); } 
+6
source share
4 answers

What you are trying to do is not possible in the repository directory, but only in the public directory, and identifying the path or URL of your laravel storage directory creates some vulnerability and its bad practice.

However, there is a way around this:

First, you need the Image package to dynamically create an image when a specific route is referenced, I recommend tampering / image , which has many methods that make the image wind-driven.

To do this, follow these steps:

1. Set the image of the intervention:

Add an intervention image to your .json composer and do a composer update

 "require": { "php": ">=5.5.9", "laravel/framework": "5.1.*", "intervention/image": "2.*", "intervention/imagecache": "2.*" ..... 

In the $ providers array, add the service providers for this package.

 'Intervention\Image\ImageServiceProvider' 

Add the facade of this package to the $ aliases array.

'Image' => 'Intervention\Image\Facades\Image '

Customize Laravel 5 intervention image

 $ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5" 

Publish configuration in Laravel 4

 $ php artisan config:publish intervention/image 

Learn more about setting https://github.com/Intervention/image

After setting the intervention / image, you can do something like this:

Image::make($path=storage_path('stock-photos/image1.jpg'));

2. Setting the route that will handle image requests

 Route::get('stock-photos/{image}', function($image){ //do so other checks here if you wish if(!File::exists( $image=storage_path("stock-photos/{$image}") )) abort(404); return Image::make($image)->response('jpg'); //will ensure a jpg is always returned }); 

3. Then in the view, you can do the following:

 @foreach($projects as $project) <li> <img src="{{url('stock-photos/'. $project->fetchMedia->first()->public_name)}}" title="{{$project->name}}"> </li> @endforeach 
+11
source

Unfortunately, I can not comment on the Digitlimit record (since I am still not high enough in reputation), but their answer worked for me, but with one change. Instead of returning Image::make($image) , I used the response() method instead, as it seemed to generate a header for me. For example,

 Route::get('stock-photos/{image}', function($image){ if(!File::exists( $image=storage_path("stock-photos/{$image}") )) abort(404); $returnImage = Image::make($image); return $returnImage->response(); }); 

Hope this helps others with a similar problem.

+2
source

when displaying images you do not need to refer to the file path. What is included in the img tag is the URL of the image you want to display. So in your case it will be

 @foreach($projects as $project) {{$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix()}} <li><img src="/{{$project->fetchMedia->first()->public_name}}" title=" {{$project->name}}"></li> @endforeach 

But. this will not work, since the resources you do http must be available for publication. (e.g. css, js-scripts, images). With this, as they say, you need to store images in another place, a public folder is an ideal place for this. If you save your images, for example, in public/images/projects , then in your view you refer to them as follows:

 <img src="/images/projects/{{$project->fetchMedia->first()->public_name}}" /> 
0
source

Although I used Laravel for about 2 years, I just recently started processing image uploads with it (using version 5.3.16). Troubleshooting image processing leads me to this message ... The original accepted answer pretty much resolved the problems I had, with some minor changes, as shown below:

 Route::get('images/books/{book_id}/{image}', function($book_id, $image){ $storagePath = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix(); $imageFilePath = $storagePath . "images/books/{$book_id}/{$image}"; if(!File::exists($imageFilePath)) abort(404); return Image::make($imageFilePath)->response(); }); 

In this particular example, I want to get a specific uploaded image for a book. For this book, I created a directory that matches the book ID, and uploaded the image there.

To get the image, I first get the shared storage directory. This is usually defined in the "filesystems.php" configuration file in the config / app directory. My example is below (missing additions are not needed for this answer):

 <?php return [ /* |-------------------------------------------------------------------------- | Default Filesystem Disk |-------------------------------------------------------------------------- | | Here you may specify the default filesystem disk that should be used | by the framework. A "local" driver, as well as a variety of cloud | based drivers are available for your choosing. Just store away! | | Supported: "local", "ftp", "s3", "rackspace" | */ 'default' => 'local', . . . 'disks' => [ 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'visibility' => 'public', ], ], ]; 

The rest of the route information follows the same pattern as the first accepted answer. The rendering of the image also follows the accepted answer.

0
source

Source: https://habr.com/ru/post/989234/


All Articles