i’m working on a php project and i’m using the laravel framework at version 8. i normally save my images in the database, but the problem is that i can’t display my images in my views.
I would like someone to help me, I have already done 3 days under.
Nb: I made a php artisan storage: link
the content of my filesystems.php file
<?php
return [
'default' => env('FILESYSTEM_DRIVER', 'local'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
my blade view
<div class="row match-height">
@foreach($services as $service)
<div class="col-xl-4 col-md-6 col-sm-12">
<div class="card">
<div class="card-content">
<img class="card-img-top img-fluid" src="{{ Storage::disk('public')->url('services/'.$service->image) }}" alt="{{ $service->title }}">
<div class="card-body">
<h5>{{ $service->title }}</h5>
<p class="card-text mb-0">{{IlluminateSupportStr::limit($service->description, '100')}}</p>
<div class="card-btns d-flex justify-content-between mt-2">
<a href="{{ route('admin.service.edit', $service->id) }}" class="btn gradient-light-primary text-white">Edit</a>
<a href="{{ route('admin.service.show',$service->id) }}" class="btn bg-gradient-info text-white">Show</a>
<button class="btn btn-outline-danger" type="button" onclick="deleteService({{ $service->id }})">{!! trans('validation.delete') !!}
</button>
<form id="delete-form-{{ $service->id }}" action="{{ route('admin.service.destroy',$service->id) }}" method="POST" style="display: none;">
@csrf
@method('DELETE')
</form>
</div>
</div>
</div>
</div>
</div>
<!-- Profile Cards Ends -->
@endforeach
</div>
the code of my controller for the registration of a service:
public function store(Request $request)
{
$this->validate($request,[
'category' => 'required',
'title' => 'required',
'description' => 'required',
'image' => 'required|mimes:jpeg,jpg,bmp,png',
]);
$image = $request->file('image');
$slug = Str::slug ($request->title);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'.uniqid('', true).'.'.$image->getClientOriginalExtension();
if (!Storage::disk('public')->exists('services'))
{
Storage::disk('public')->makeDirectory('services');
}
$Imageservice = Image::make($image)->resize(1600,479)->stream();
Storage::disk('public')->put('services/'.$imagename,$Imageservice);
} else {
$imagename = "default.png";
}
$service = new Service();
$service->category_id = $request->category;
$service->title = $request->title;
$service->slug = $slug;
$service->description = $request->description;
$service->image = $imagename;
$service->save();
Toastr::success('Service Successfully Saved :)','Success');
return redirect()->route('admin.service.create');
}
2
Answers
if you dont have any problem with storing the data and storage is linked with public path properly, you can write something like this to get the image from storage..
first of all to access img or any file from storage, you have link storage with public folder…
using above command it will create shortcut of storage in public folder.
then for display images you can using as like below