why not update the picture? laravel 5.4 - image

I can upload a picture, but when I try to update it, then there is no error. The file name changes in the database to the name of the new picture. And in the public folder the picture remains old and does not appear new.
I use: Intervention Image
What I did not understand, help please.
Controller: UploadController
use Illuminate\Http\Request;
use App\Post;
use Image;
use Storage;
use Faker\Provider\File;
public function update (Request $request, $id)
{
//validate
$this->validate($request, [
'title' => 'required|max:255',
'author' => 'required',
'text' => 'required',
'desc' => 'required',
'image' => 'required',
]);
$posts = Post::find($id);
$posts->title = $request->input('title');
$posts->author = $request->input('author');
$posts->text = $request->input('text');
$posts->desc = $request->input('desc');
$posts->image = $request->input('image');
//update image
if ($request->hasFile('image'))
{
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/'. $filename);
Image::make($image)->resize(800, 400)->save($location);
$oldFilename = $posts->image;
//update db
$posts->image = $filename;
//delete old image
Storage::delete($oldFilename);
}
$posts->save();
return redirect('/');
}
View: edit.blade.php
<div class="container">
<form method="POST" action="{{ route('goUpdate', [$posts->id]) }}">
{{ csrf_field() }}
{!! method_field('patch') !!}
#if($posts)
<div class="form-group">
<br>
<label>title</label>
<input name="title" type="text" class="form-control" value="{{ $posts->title }}">
</div>
<div class="form-group">
<label>author</label>
<input name="author" type="text" class="form-control" value="{{ $posts->author }}">
</div>
<div class="form-group">
<label>text</label>
<textarea name="text" class="form-control" rows="7">{{ $posts->text }}</textarea>
</div>
<div class="form-group">
<label>desc</label>
<textarea name="desc" class="form-control" rows="5">{{ $posts->desc }}</textarea>
</div>
<div class="form-group">
<label>image</label>
<input type="file" name="image" class="form-control-file" value="" >
</div>
<div>
<input name="submit" type="submit" class="btn btn-primary" value="update"/>
back
</div>
#endif
</form>
<br>
filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
'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'),
],

Try using this
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalName();
$path = public_path('events/' . $filename);
Image::make($image->getRealPath())->resize(300, 300)->save($path);
$event->image = 'events/' . $filename;
Hope this can help you

Related

laravel 9 has defferent location then i set, and every file i put become .tmp

i dont understand why, i do it in laravel 8 and it work but not in laravel 9
Store function
public function store(Request $request)
{
$ValidatedData = $request->validate([
'title' => 'required|max:255',
'slug' => 'required|unique:menus',
'categories_id' => 'required',
'deskripsi' => 'required',
'is_order' => 'required',
'most' => 'required',
'price' => 'required',
'image' => 'image|file|max:1024'
]);
if($request->file('image')){
$validatedData['image'] = $request->image->store('menus');
}
$ValidatedData['users_id'] = Auth::user()->id;
$ValidatedData['excerpt'] = Str::limit(strip_tags($request->deskripsi), 100);
Menus::create($ValidatedData);
return Redirect('/dashboard/menus')->with('success', 'Tugas Baru Telah Ditambahkan!');
}
Form
<div class="mb-3">
<label for="image" class="form-label">Size Max. 1mb</label>
<input class="form-control #error('image') is-invalid #enderror" type="file"
name="image" id="image">
#error('image')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
.env
FILESYSTEM_DISK=public
After Create the storage location is change

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);

cannot update image when edit data on laravel

there is no problem when I do create new data. everything is ok including uploading image. My image inserted to public/image directory.
But when i try editing or updating, i have a problem. my image that should be inserted to public/image not work on update function.
the controller is below
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'sequence' => 'required',
'image' => 'required|image|max:2048',
'link' => 'required',
'status' => 'required',
]);
$image = $request->file('image');
$new_name = $request->name .rand(). '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$form_data = array(
'name' => $request->name,
'sequence' => $request->sequence,
'image' => $new_name,
'link' => $request->link,
'status' => $request->status,
);
Banner::create($form_data);
return redirect()->route('banner.index');
}
public function update(Request $request, Banner $banner)
{
$image_name = $request->hidden_image;
$image = $request->file('image');
if($image != ''){
$request->validate([
'name' => 'required',
'sequence' => 'required',
'image' => 'image|max:2048',
'link' => 'required',
'status' => 'required',
]);
$image_name = $request->name .rand(). '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $image_name);
}
else{
$request->validate([
'name' => 'required',
'sequence' => 'required',
'link' => 'required',
'status' => 'required',
]);
}
$form_data = array(
'name' => $request->name,
'sequence' => $request->sequence,
'image' => $image_name,
'link' => $request->link,
'status' => $request->status,
);
Banner::whereId($banner)->update($form_data);
return redirect()->route('banner.index');
}
And my view code is below
<form action="{{ route('banner.update',$banner->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row" style="margin-top: 10px">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $banner->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Sequence</strong>
<input type="number" name="sequence" value="{{ $banner->sequence }}" class="form-control" placeholder="Sequence">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Image</strong>
<div class="col-md-8">
<img src="{{ URL::to('/') }}/images/{{ $banner->image }}" class="img-thumbnail" width="200" />
</div>
<div class="col-md-8">
<input type="file" name="image" />
<input type="text" name="hidden_image" value="{{ $banner->image }}" />
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Link</strong>
<input type="text" name="link" value="{{ $banner->link }}" class="form-control" placeholder="Link">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Status</strong>
<input type="text" name="status" value="{{ $banner->status }}" class="form-control" placeholder="Status">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
your form is missing enctype attribute and so no file is passing with the form. add that attribute and everything will work.
<form action="{{ route('banner.update',$banner->id) }}" method="POST" enctype="multipart/form-data">

The image failed to upload.on a server laravel

The image failed to upload.
link
https://comedoruniversitariouncp.000webhostapp.com/products/create
The project works in local server,
the error appears when i upload to a server
create.blade.php
<form action="{{ route('products.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group row">
<label class="col-form-label col-sm-2">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name">
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Price</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="price" step="0.1">
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Amount</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="amount" >
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Image</label>
<div class="col-sm-10">
<input type="file" class="form-control-file" name="image">
</div>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
ProductController.php
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'price' => 'required',
'amount' => 'required',
'image' => 'required|image'
]);
$image = $request->file('image');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
Product::create([
'name' => $request->name,
'price' => $request->price,
'amount' => $request->amount,
'image' => $new_name
]);
return redirect()->route('products.index')->with('message', 'Product created successfully');
}
As you mention about works on local but not remote. I assumed that the upload_max_filesize is greater than the size your upload file, and both on local and remote are not the same.
You may use The Storage Facade as a convenient way to interact with your local filesystems.
use Illuminate\Support\Facades\Storage;
//...
$new_name = rand() . '.' . $image->getClientOriginalExtension();
Storage::disk('public')->putFileAs('images', request->file('image'), $new_name);
//...
Docs
You should try this
Try with adding mimetypes in validation of image.
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'price' => 'required',
'amount' => 'required',
'image' => 'required|mimes:jpeg,bmp,png'
]);
$image = $request->file('image');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
Product::create([
'name' => $request->name,
'price' => $request->price,
'amount' => $request->amount,
'image' => $new_name
]);
return redirect()->route('products.index')->with('message', 'Product created successfully');
}
You Should Try code this,
you may change a part code :
$image->move(public_path('images'), $new_name);
to be code :
$image->move(public_path('images'.$new_name));
this is code, 100% work to me.

Tables Only Update When All Fields Have Been Changed

If I change just the user table items it'll update them (if i change everything in the user table rows). However, it'll only update the user_infos table when I edit everything.
ProfilesController.php
public function update(Request $request)
{
$user_id = Auth()->user()->id;
$id = Auth()->user()->id;
$this->validate($request, array(
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'type' => 'required|string',
'description' => 'string',
'projects' => 'string',
'experience' => 'string',
'links' => 'string',
'status' => 'string'
));
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 260)->save( public_path('/images/uploads/avatars/' . $filename ) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
$user = User::find($id);
$user->first_name = $request->input('first_name');
$user->last_name = $request->input('last_name');
$user->email = $request->input('email');
$user->type = $request->input('type');
$user_info = User_Info::find($user_id);
//$user_info = User_Info::where('user_id', $user->id)->first();
$user_info->description = $request->input('description');
$user_info->projects = $request->input('projects');
$user_info->experience = $request->input('experience');
$user_info->links = $request->input('links');
$user_info->status = $request->input('status');
$user->save();
$user_info->save();
return redirect()->route('profile')->withUser($user);
}
settings.blade.php
<div class="twelve wide column">
<div class="ui segment" data-tab="bio">
<form action="{!! action('ProfilesController#update', ['id' => $user->id]) !!}" method="POST" enctype="multipart/form-data" class="ui form">
{{ csrf_field() }}
<h3>Name</h3>
<div class="inline fields">
<div class="eight wide field">
<input type="text" name="first_name" placeholder="First Name" value="{{ $user->first_name }}">
</div><!-- ./Eight Wide Field -->
<div class="eight wide field">
<input type="text" name="last_name" placeholder="Last Name" value="{{ $user->last_name }}">
</div> <!-- ./Eight Wide Field -->
</div><!-- ./Inline Fields -->
<h3 class="header">Profile Image</h3>
<div class="inline fields">
<div class="sixteen wide field">
<input type="file" name="avatar">
</div> <!-- ./Sixteen Wide Field -->
</div> <!-- ./Inline Fields -->
<h3 class="header">Email</h3>
<div class="inline fields">
<div class="sixteen wide field">
<input type="email" name="email" placeholder="Email" value="{{ $user->email }}">
</div> <!-- ./Sixteen Wide Field -->
</div> <!-- ./Inline Fields -->
<h3>Type</h3>
<div class="inline fields">
<div class="sixteen wide field fluid">
<select name="type" class="ui dropdown fluid registerType">
<option value="Developer">Developer</option>
<option value="Designer">Designer</option>
<option value="Fullstack">FullStack</option>
<option value="Client">Client</option>
</select>
</div> <!-- ./Sixteen Wide Field Fluid -->
</div><!-- ./Inline Fields -->
<h3 class="header">Description</h3>
<textarea name="description" id="" cols="30" rows="10" placeholder="Description">{{ isset($user->userInfo->description) ? $user->userInfo->description : "This User Has No Description" }}</textarea>
<h3 class="header">Projects</h3>
<textarea name="projects" id="" cols="30" rows="3" placeholder="Projects">{{ isset($user->userInfo->projects) ? $user->userInfo->projects : "This User Has No Projects Listed" }}</textarea>
<h3 class="header">Experience</h3>
<textarea name="experience" id="" cols="30" rows="3" placeholder="Experience">{{ isset($user->userInfo->experience) ? $user->userInfo->experience : "This User Has No Experience Listed" }}</textarea>
<h3 class="header">Links</h3>
<input type="text" name="links" placeholder="Links" value="{{ isset($user->userInfo->links) ? $user->userInfo->links : "This User Has No Links Listed" }}">
<h3 class="header">Status</h3>
<input type="text" name="status" placeholder="Status" value="{{ isset($user->userInfo->status) ? $user->userInfo->status : "Available" }}">
<br><br>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="ui medium fluid blue button" value="Update">
{!! Form::close() !!}
</div><!-- ./Ui Segment -->
</div> <!-- ./Twelve Wide Column -->
Thanks in advance!
Using: Laravel 5.4
In here:
$user->update(); // <--- try this
$user->save();
$user_info->save();
In my ProfilesController.php I had to change the validation for "email" inside public function update(Request $request) a little bit.
From:
$this->validate($request, array(
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'type' => 'required|string',
'description' => 'string',
'projects' => 'string',
'experience' => 'string',
'links' => 'string',
'status' => 'string',
));
To:
$this->validate($request, array(
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,'.$id,
'type' => 'required|string',
'description' => 'string',
'projects' => 'string',
'experience' => 'string',
'links' => 'string',
'status' => 'string',
));

Resources