Laravel can not display image - laravel

trying to upload an image through a post Form:
this is my controller:
public function store(Request $request)
{
$request->validate([
'title'=>'required',
'content'=>'required',
'image'=>'required',
'description'=>'required'
]);
$image = $request->image->store('images');
Post::create([
'title' => $request->input('title'),
'content' => $request->input('content'),
'description' => $request->input('description'),
'image' => $image
]);
return redirect(route('posts.index'))->with('success', 'Posts Submitted successfully');
}
I used : php artisan storage:link which generated a folder named images in public directory: Absolute path is:
C:\xampp\htdocs\blogPholio\public\storage\images\KWDKRrty2ijXjrgrismvyiV4k6UAQNrFogJl9W2f.jpeg
in my blade I am trying to show image through/:
<img src="{{Storage::url($post->image)}}">
My form in the create blade looks like:
<form action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Image:</strong>
<input type="file" name="image" class="form-control" placeholder="upload:">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</form>
Thanksa lot

Try this
<img src="{{asset(Storage::disk('local')->url($post->image))}}"/>

Related

How to allow only jpg,jpeg,png image only in laravel?

Controller
public function store_room_detail(Request $request)
{
$request->validate([
'room_image' => 'required|mimes:png,jpg,jpeg|max:2048'
]);
return redirect()->back();
}
View:
<form method="POST" action="{{ route('admin.store-room-detail') }}" enctype="multipart/form-data">
#csrf
<div class="card-body">
<div class="form-group">
<label for="room_image">Room Image</label>
<input type="file" class="col-md-6" id="room_image" name="room_image[]">
<a href="javascript:void(0)" class="btn btn-primary add_more mb-3">
Add More
</a>
<div class="wrap"></div>
#if ($errors->has('room_image'))
<span class="text-danger">{{ $errors->first('room_image') }}</span>
#endif
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
In the above code. Validation working fine but when I upload jpg, png image then it will again show that The room image must be a file of type: png, jpg, jpeg. I don't why? Please help me.
Thank You
you should validate like this because you send an array in request:
$request->validate([
'room_image' => 'required',
'room_image.*' => 'required|mimes:png,jpg,jpeg|max:2048'
]);

Can't store Files in public folder in Laravel 8

i'm trying to store pdf, txt... in DB
but i get this error:
Method Illuminate\Validation\Validator::validateCsv,txt,xlx,xls,pdf does not exist.
but when i add required|mimes this error i get:
The file must be a file of type: pdf, xlx, csv, txt, doc.
here the code:
public function store(Request $request)
{
$validatedData = $request->validate([
'file' => 'required|mimes:pdf,xlx,csv,txt,doc'
]);
$name = $request->file('file')->getClientOriginalName();
$path = $request->file('file')->store('public/files');
$save = new File;
$save->name = $name;
$save->path = $path;
return redirect('file-upload')->with('status', 'File Has been uploaded successfully');
}
my blade code:
<body>
<div class="container mt-4">
<form method="post" enctype="multipart/form-data" id="upload-file" action="{{ url('store') }}" >
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="file" name="file" placeholder="Choose file" id="file">
#error('file')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-primary" id="submit">Submit</button>
</div>
</div>
</form>
</div>
</div>
</body>
how can i fix it?
Use File to put content
\File::put(public_path('storage'). '/public/files/'. $file);

How to add form action to laravel function

I have a website I am currently editing tht was built with laravel. I have have a page that displays a "details of shipped package"
I added a form to page to update the current location of the shipped package on the details page.
<div class="row mb-30">
<div class="col-lg-12 mt-2">
<div class="card border--dark">
<h5 class="card-header bg--dark">#lang('Courier Location')</h5>
<div class="card-body">
<form action="{{route('....')}}" method="POST">
#csrf
<div class="modal-body">
<div class="form-group">
<label for="current_location" class="form-control-label font-weight-bold">#lang('Current Location')</label>
<input type="text" class="form-control form-control-lg" name="current_location" value="{{__($courierInfo->current_location)}}" required="">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn--primary"><i class="fa fa-fw fa-paper-plane"></i>#lang('Update')</button>
</div>
</form>
</div>
</div>
</div>
I have also added the update function in the controller
public function courierUpdate(Request $request, $id)
{
$request->validate([
'current_location' => 'required',
]);
$courierInfoUpdate =CourierInfo::findOrFail($id);
$courierInfoUpdate->current_location = $request->current_location;
$courierInfoUpdate->save();
$notify[] = ['success', 'Courier location info has been updated'];
return back()->withNotify($notify);
}
I am having problem with the laravel route to call that should be added as form action.
Declare a route on the web.php
Route::post('/courier-Update/{id}','App\Http\Controllers\YourControllerName#courierUpdate')->name('courier.Update');
and now just call this route in your form and also pass the id of that courier
like this:
route('courier.Update',$courier->id)
You can add a new route in routes/web.php
//import your controller at Beginning of the file
use App\Http\Controllers\YourController;
Route::post('update_location/{id}', [YourController::class, 'courierUpdate'])->name('updateLocation');
//or
Route::post('update_location/{id}', 'YourController#courierUpdate')->name('updateLocation');
And then in your blade view
<form action="{{ route('updateLocation', [ 'id' => $id]) }}" method="POST">
#csrf
</form>

Change image name and save to DB Laravel

Hi i can not store image name to database in Laravel project.
How to solve this?
Here is codes of controller
class TarifController extends Controller
{
public function store(Request $request)
{
$request->validate([
'title_uz' => 'required',
'desc_uz' => 'required',
'full_desc_uz' => 'required',
'company_id' => 'required',
'order' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,svg|max:2048',
]);
$image1 = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $image1);
Tarif::create($request->all());
return redirect()->route('tarifs.index')
->with('success','Yangi tarif muvoffaqiyatli qo`shildi.');
}
}
and here is codes from view
<form role="form" action="{{ route('tarifs.store') }}" method="post" enctype="multipart/form-data">
#csrf
<div> .. another fields .. </div>
<div class="col-5">
<label for="image">Surat</label>
<input type="file" class="form-control" name="image" id="image" required>
</div>
<div> .. another fields .. </div>
<div class="card-footer">
<a class="btn btn-info" href="{{ route('tarifs.index') }}">Qaytish</a>
<button type="submit" class="btn btn-success">Saqlash</button>
</div>
</form>
It saves image to public/images folder but didn't saves filename or path to DB. The field name is 'image' on database.
If you need to merge new values into a request object, the following code would have done the trick :
$request->merge(['image' => 'avatar.png']);
Or, you can change your code like this :
$image1 = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $image1);
$input = $request->all();
$input['image'] = $image1;
Tarif::create($input);

Image source not readable in AbstractDecoder.php line 346 at AbstractDecoder->init({image path})

I used intervention image to resize image for a laravel 6 app. After developing on local server, which worked perfectly, i try uploading to a shared hosting, but i am getting errors.
Image source not readable
I have tried everything i saw on stackoverflow, and laracast. but they are not working for me.
i try
changing the base path in index.html
Running storage link, in Appserviceprovider
removing public_html(). worked but not saving image to public_html->storage folder
dd() the request paths, its correct
my codes:
index.php:
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$app->bind('path.public', function() {
return __DIR__;
});
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$app->bind('path.public', function() {
return __DIR__;
});
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
Post Controller that handles image uploads
$image = $request->file('image');
$imagePath = $image->store('posts', 'public');
$image = Image::make(public_path($request->file('image')->getRealPath()))->fit(1263, 864);
return $image;
$image->save();
BlogPost::create([
'title' => $request->title,
'image' => $imagePath,
'categories' => json_encode($request->categories),
'isEvent' => $request->isEvent,
'isEditorial' => $request->isEditorial,
'body' => $request->body,
]);
<form enctype="multipart/form-data" action="{{ route('post.store')}}" method="POST">
<div class="container">
#method('post')
#csrf
<div class="col-md-12">
<div class="card card-outline card-info">
<div class="card-body pad">
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">Add blog post</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-sm-12 col-md-6">
<div class="form-group">
<label for="customFile">Post Title</label>
<input type="text" class="form-control" name="title" id="title" maxlength="250">
<small id="character_txt" class="text-success"><span id="characters">255</span> characters left</small>
</div>
</div>
<div class="col-sm-12 col-md-6">
<label for="customFile">Upload Post image</label>
<div class="input-group input-group-md">
<input type="file" name="image" class="form-control">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
The Image source not readable is pointing to the make(). i have confirm the image path has the correct path.
Error page
I was having the same problem with Intervention and Laravel 6. After lots of research that never helped me, I did the following and it worked.
Delete the storage link folder inside public
Launch a terminal in your cpanel, cd to your project and run php artisan storage:link
I then tried uploading once again and it worked just fine

Resources