I'm create project CRUD with uploads file using json_encode for uploads file in database. i want to ask how to edit/update json field in database laravel ?
i try using isset($data)? json_encode($data): $sid->file_uploads in public function update() but that code when i update can be replace previous file.
$sid = Sid::find($id);
if($request->file('file_uploads'))
{
foreach($request->file('file_uploads') as $file)
{
$name = $file->getClientOriginalName();
$path = 'public/file/'.$sid->employee_name;
$file->move($path, $name);
$data[] = $name;
}
}
$sid->employee_sid = $request->employee_sid;
$sid->employee_npk = $request->employee_npk;
$sid->employee_name = $request->employee_name;
$sid->file_uploads = isset($data)? json_encode($data): $sid->file_uploads;
$sid->save();
i expect the result is file when i upload will add file in DB. for example i uploads 5 file but when i create i just uploads 2 file and when i want to edit i add 3 file and totals file is 5 in DB.
This can resolve, php in the new versions does not support creation of var at runtime, or its if that checks if it has files is resulting in false.
$sid = Sid::find($id);
$data = [];
if($request->file('file_uploads'))
{
foreach($request->file('file_uploads') as $file)
{
$name = $file->getClientOriginalName();
$path = 'public/file/'.$sid->employee_name;
$file->move($path, $name);
$data[] = $name;
}
}
$sid->employee_sid = $request->employee_sid;
$sid->employee_npk = $request->employee_npk;
$sid->employee_name = $request->employee_name;
$sid->file_uploads = count($data)? json_encode(array_merge(json_decode($sid->file_uploads, true),$data)): $sid->file_uploads;
$sid->save();
Related
why my old image can't be deleted when i uploaded new image?
this is my controller where i use to store data and image.
my file image name is stored combination from nama_post_adps and from $imageName where that is query for getClientOriginalName();
public function update(Request $request, Adopsi $adopsi, $id)
{
$getParentJenisHewan = JenisHewan::where("id", $request->jenis_hewan_id)->value("nama_jenis_hewan");
$getParentRasHewan = RasHewan::where("id", $request->ras_hewan_id)->value("nama_ras_hewan");
$imageSize = $request->file('image_post_adps')->getSize();
$imageName = $request->file('image_post_adps')->getClientOriginalName();
$pathstorage = $request->file('image_post_adps')->storeAs('public/post/adopsi',$request->nama_post_adopsi.'-'.$imageName);
$adopsiAttr = $this->validasiRequest();
$adopsiAttr = $request->all();
$adopsiAttr['nama_jenis_hewan'] = $getParentJenisHewan;
$adopsiAttr['nama_ras_hewan'] = $getParentRasHewan;
$adopsiAttr['image_post_adps'] = $imageName;
$adopsiAttr['path-storage'] = $pathstorage;
$adopsiAttr['size'] = $imageSize;
$adopsi = Adopsi::find($id)->update($adopsiAttr);
$delete = Adopsi::find($id);
unlink('app/public/post/adopsi/'.$delete->nama_post_adopsi.'-'.$delete->image_post_adps);
// if ($request->file('image_post_adps')) {
// Storage::disk('public')->delete('/post/adopsi/'.$request->nama_post_adopsi.'-'.$request->image_post_adps);
// }
if($adopsi) {
return redirect()->route('adopsi.index')->with('success','Data '.$request->nama_post_adopsi .' telah mendapatkan update terbaru.');
} else {
return redirect()->route('adopsi.index')->with('error','Data gagal'.$request->nama_post_adopsi .'diupdate');
}
}
method above literally same with my store method .
i've try unlink() but it can't work and show error : unlink('post/adopsi/..') no such file or directory , but field can't be updated. then i decided use Storage::disk('public')->delete , field got update, the image change with new but old still same.
You are doing wrong.
First fetch existing data and check if request has file then delete old one then update it
$delete = Adopsi::find($id);
if ($request->file('image_post_adps')) {
Storage::disk('public')->delete('/post/adopsi/'.$delete->nama_post_adopsi.'-'.$delete->image_post_adps);
}
$adopsi = Adopsi::find($id)->update($adopsiAttr);
Currently I have single upload of image using Laravel Passport API
I have this code and it's working fine.
//Saves file to public folder
$dateTime = date('Ymd_His');
$file = $request->file('file');
$fileName = $dateTime . '-' . $file->getClientOriginalName();
$savePath = public_path('/upload/img/');
$file->move($savePath, $fileName);
//This saves the current file path of image to mytable
$ActivityLog = new ActivityLogImg;
$ActivityLog->actCode = $activity_code;
$ActivityLog->projCode = $request->projCode;
$ActivityLog->attachment = "/upload/img/".$fileName;
$ActivityLog->type = "IMAGE";
$ActivityLog->deleted = 0;
$ActivityLog->created_by_id = Auth::user()->company_id;
$ActivityLog->created_by_name = Auth::user()->name;
$ActivityLog->created_at = now();
$ActivityLog->updated_at = now();
$ActivityLog->save();
return response([
"status"=>"ok",
"message"=>"Activity successfully submitted!"
]);
and I have this postman request to test the api and it's working fine
Now I'm trying to do multiple upload of image in one single request. Is that possible for this code?
yes can do it same way with your code
in postman pass name as file[] as multiple time
foreach($request->file('file') as $file){
$dateTime = date('Ymd_His');
$fileName = $dateTime . '-' . $file->getClientOriginalName();
$savePath = public_path('/upload/img/');
$file->move($savePath, $fileName);
//This saves the current file path of image to mytable
$ActivityLog = new ActivityLogImg;
$ActivityLog->actCode = $activity_code;
$ActivityLog->projCode = $request->projCode;
$ActivityLog->attachment = "/upload/img/".$fileName;
$ActivityLog->type = "IMAGE";
$ActivityLog->deleted = 0;
$ActivityLog->created_by_id = Auth::user()->company_id;
$ActivityLog->created_by_name = Auth::user()->name;
$ActivityLog->created_at = now();
$ActivityLog->updated_at = now();
$ActivityLog->save();
}
return response([
"status"=>"ok",
"message"=>"Activity successfully submitted!"
]);
In my Laravel project I created a page to upload the files and I use the $file of laravel it works fine for some system only but for some system it shows an error as shown in image below.
Function I am using to upload files in model
public function add_document_sub_cert($req)
{
$subcontractor_id = $req['subcontractor_id'];
$reference_id = $req['reference_id'];
$files = $req->file("uploaded_doc0");
$i = 0;
foreach($files as $file){
$i++;
$ext = $file->guessClientExtension();
$name = $file->getClientOriginalName();
$file_name_1 = str_replace(".".$ext,"",$name);
$path = $file->storeAs('subcontractor/','avc'.$i.'.jpg');
if($path){
$document = new Document();
$document->doc_name = 'avc.jpg';
$document->module = 'subcontractor';
$document->reference_id = $reference_id;
$document->save();
}
}
}
Your error says that you didn't specify a filename. I see that your variable $file_name_1 is never used. Haven't you forgotten to use it somewhere?
Without knowing how your class Document works, it's impossible to tell you exactly where is the bug.
I am trying to save fine path in database and save image file in folder but I want to save file with unique name. How to do that?
My controller is :
$image = $request->photo;
//$photoname_path = 'images'.$date.'/'.$image->getClientOriginalName();
$image->move('images',$image->getClientOriginalName());
$photoname = 'images/'.$image->getClientOriginalName();
$adduser = new Employee;
$adduser->name = $request->name;
$adduser->photo = $photoname;
$adduser->email = $request->email;
$adduser->contact_no = $request->contact_no;
$adduser->pan_no = $request->pan_no;
$adduser->department = $request->department;
$adduser->position = $request->position;
$adduser->save();
return redirect()->back();
I was trying to save it with time but I failed. How to do it?
I see that you have access to the request object so, try this:
$photo = $request->file('photo');
$photoName = time() . '_' . $photo->getClientOriginalName();
$photo->move('images', $photoName);
PHP question on how to return a specific value from another class in a seperate file. So I have created a uploader class to handle and store files uploaded by users. But trying to connect the dots back to the orginal request which if for lets say a specific page - how can i pass a specific value back after I set the values in the other file... code example below.
Thanks Citti
//file 1 //store page request
public function store()
{
//pass the uploaded file to be uploaded and stored
$this->uploader->upload(RequestFile::file('assetfile'));
$page = new Page;
$page->user_id = $asset->user()->id;
$page->assetfile = $file->user_id; <--- ? see below
$page->save();
}
//file 2 //store physical file and create record in DB
class Uploader {
public function upload($file)
{
$extension = $file->getClientOriginalExtension();
$string = str_random(30);
$uploadName = $string . '.' . $extension;
$destinationPath = storage_path() . '/app/uploads/' . Request::user()->id;
$file->move($destinationPath, $uploadName);
$file = new File;
$file->user_id = $asset->user()->id;
$file->assetfile = $uploadId;
$file->save();
$return $file->user_id; ?!?
What you could try is session variables?
Be sure to start sessions on both file 1 and file 2 via:
session_start();
Now, in your upload function, you can declare a session variable
$_SESSION['specificValue'] = $valueSetInUpload;
Now, back on your first file you can access the variable inside of your store function with
$valueSetInUpload = $_SESSION['specificValue'];
I believe this is what you are asking, but I could be wrong.
To return values from a function, you use "return value" and you just have to store that to a variable or output it. Something like:
public function store() {
$fileId = $this->uploader->upload(RequestFile::file('assetfile'));
$page = new Page;
$page->user_id = $asset->user()->id;
$page->assetfile = $fileId;
$page->save();
}
//file 2 //store physical file and create record in DB
class Uploader {
public function upload($file) {
$extension = $file->getClientOriginalExtension();
$string = str_random(30);
$uploadName = $string . '.' . $extension;
$destinationPath = storage_path() . '/app/uploads/' . Request::user()->id;
$file->move($destinationPath, $uploadName);
$file = new File;
$file->user_id = $asset->user()->id;
$file->assetfile = $uploadId;
$file->save();
return $file->user_id;
}
}