Reputation: 1146
In storage i have /projects/(nameofproject - not always the same)/header.jpg and also have /projects/(nameofproject - not always the same)/home.png.
How can i show them in my view?
I explain it a bit more. In the past, i have projects on /public/assets/img/projects and now i do it dynamically.
My function to create the new projects and save the images is this:
public function storeProject(Request $request)
{
$project = new Project();
$project->slug = $request->input("slug");
$project->position = $request->input("position");
$project->public = $request->input("public");
$project->pathheader = $request->file('pathheader');
$project->pathhome = $request->file('pathhome');
\Storage::disk('projects')->makeDirectory($project->slug);
\Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.png');
\Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.png');
$project->save();
return redirect('/admin/projects');
}
The images of the projects i do in the past i show it with this: (I pass to the view the variable $project)
<p class="lead"><img src="/assets/img/projects/{{$project->slug}}/header.jpg" width="50%" >Header</p>
<p class="lead"><img src="/assets/img/projects/{{$project->slug}}/home.jpg" width="50%">Home</p>
How to show the images of the storage folder?
EDIT
filesystems.php code:
'projects' => [
'driver' => 'local',
'root' => storage_path() . '/projects',
'visibility' => 'public',
],
View looking like this now:
@if (Storage::disk('projects')->has($project->slug))
<img src="{{ asset('storage/projects/'.$project-
>slug.'/header.jpg') }}" width="50%" >
@else
<p class="lead"><img src="/assets/img/projects/{{$project-
>slug}}/header.jpg" width="50%" >Header</p>
@endif
I make:
php artisan storage:link
url of image: http://url.loc/storage/projects/hola-que-tal/header.jpg (not show it).
Upvotes: 1
Views: 1642
Reputation: 5082
The easiest solution is to use the public disk. If you ran the php artisan storage:link
command you can use the following code:
\Storage::disk('public')->makeDirectory($project->slug);
\Storage::disk('public')->putFileAs($project->slug, $project->pathheader, 'header.png');
\Storage::disk('public')->putFileAs($project->slug, $project->pathhome, 'home.png');
And your HTML:
<p class="lead"><img src="{{ asset('storage/projects/'.$project->slug.'/header.jpg') }}" width="50%" >Header</p>
<p class="lead"><img ssrc="{{ asset('storage/projects/'.$project->slug.'/home.jpg') }} width="50%">Home</p>
An example for the config in config/filesystems.php
:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public/projects'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Of course you can edit your projects
disk so that it has the correct root
. But I'm not sure how your config looks for the projects
disk.
Upvotes: 1