Laravel retrieve images from database - php

I need to display the stored images in my view.I've used the below code to store image in db,
$file = Input::file('pic');
$img = Image::make($file);
Response::make($img->encode('jpeg'));
$picture = new Car;
$picture->cartype = Input::get('cartype');
$picture->price = Input::get('price');
$FileName = Input::file('pic')->getClientOriginalName();
$FilePath = public_path('') .'/images/'. $FileName;
$picture->name = $FilePath;
$picture->save();
In my controller( to retrieve image)
public function index()
{
$cars = DB::table('car_category')->get();
return view('category.category',['cars'=>$cars]);
}
How should I retrieve and display the images in view?

If you are storing the image name in the database and uploading it in a directory then you can easily display it by simply fetching the name from database, returning the name to view and append the name to a hard-coded directory address like this -
<img src="/images/{{ $imageVariableName }}">
Let me know if this works.

Related

Dynamic Input Array with Image/file in Laravel 8

i am trying to do an update using dynamic for "education history" but there is an image also, i use eloquent to update too, for some reason the data is saved in the database but the image just tmp and cannot find in the folder.
//count array data inputed
for($incs=0; $incs < count($data_detail_user['institution_names']); $incs++) {
// update education
foreach( $data_detail_user['institution_names'] as $key => $file ){
// get old photo thumbnail
$get_photo = Educations::where('id', $key)->first();
// store photo
$path = $file->store(
'assets/education/thumbnail', 'public'
);
$education_user = Educations::find($incs);
$education_user->detail_user_id = $detail_user['id'];
$education_user->name = $data_detail_user['institution_names'][$incs];
$education_user->course = $data_detail_user['education_courses'][$incs];
$education_user->start = $data_detail_user['education_starts'][$incs];
$education_user->graduate = $data_detail_user['education_graduates'][$incs];
$education_user->address = $data_detail_user['education_addresses'][$incs];
$education_user->regencies = $data_detail_user['education_regencies'][$incs];
$education_user->provinces = $data_detail_user['education_provinces'][$incs];
$education_user->country = $data_detail_user['education_countries'][$incs];
$education_user->zip_code = $data_detail_user['education_zips'][$incs];
$education_user->certificate = $data_detail_user['education_certificates'][$incs][$path];
$education_user->save();
$data_detail_user = 'storage/' .$get_photo['certificate'];
if (File::exists($data_detail_user)) {
File::delete($data_detail_user);
} else {
File::delete('storage/app/public/' .$get_photo['certificate']);
}
}
}
[the error show Call to a member function store() on string]
this is the screenshoot of the error
[1]: https://i.stack.imgur.com/l96sm.png
$path = $request->file('path')->store('public/post');
I assume you have a directory where the image is suppose to be stored that's why i created the post directory after public.

Storing Image path using Laravel

I have a controller which handles the upload functionality of the music file. The controller uses this Laravel getID3 package to parse the metadata from the music file and store in the database.
My code looks like this
if($request->hasfile('songs')){
foreach ( $request->file('songs') as $key => $file){
$track = new getID3($file);
$tifo = $track->extractInfo();
$artistName = $track->getArtist();
$songName = $track->getTitle();
$albumName = $track->getAlbum();
$extension = $track->getFileFormat();
$thumbnail = $track->getArtwork(true);
$thumbnails = 'artwork-'.time().'.'.$thumbnail->getClientOriginalExtension();
$location = time() .uniqid().'.' . $extension;
$file->storeAs('public/songs',$location);
//$file->storeAs('public/sthumbs',$thumbnails);
$file = new MusicUpload();
$music_upload_file = new MusicUpload();
$music_upload_file->user_id = Auth::user()->id;
$music_upload_file->filename = $songName;
$music_upload_file->extension = $extension;
$music_upload_file->artistname = $artistName;
$music_upload_file->albumname = $albumName;
$music_upload_file->location = $location;
$music_upload_file->thumbnail = $thumbnails;
$music_upload_file->save();
}
}
What I want to do is to store both the music file as well as the thumbnail of the file in the database.
Here the $thumbnails will store the image in the specified folder but the image is unreadable, i.e. it has the same file size as the music and doesn't contain the artwork which is to be stored and retrieved.
If I don't include the $thumbnails part, the package default stores to the temp folder which is inaccessible to the controller.
So how do I write the code such that the thumbnail(artwork) of the music gets stored in the correct folder and it can display the output too.
You can store it to storage folder.
Storage::disk('public')->put($location,File::get($file));
Don't forget to run php artisan storage:link

laravel Can't delete old image when uploaded new image

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

How to retrive single field array data from database and separate them- using laravel, PHP

I Trying to upload multiple image for my product, and store the names of that images in database as an array.
My code works like this-
$images = $request->file('images');
if (isset($images)) {
foreach($images as $image){
$imagename = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if (!file_exists('uploads/product/images')) {
mkdir('uploads/product/images', 0777, true);
}
$image->move('uploads/product/images', $imagename);
$data[] = $imagename;
}
}else{
$data[] = 'default.png';
}
$product = new Product();
$product->images = json_encode($data);
And data stored inside the images field like-
["jeans-2020-08-13-5f352f18b30a4.jpg","jeans-2020-08-13-5f352f18b36a0.jpg","jeans-2020-08-13-5f352f18b3a2c.jpg"]
**And the problem is how can i separate this image name to show images in Laravel Blade? **
OR Suggest me, If there is another way to upload multiple image or multiple value in laravel-6
You need to deserialize the images names and then loop through them:
#foreach(json_decode($product->images) ?? [] as $image)
<img src="uploads/product/images/{{ $image }}">
#endforeach

How to upload and save multiple images in database with laravel 5.4?

im trying to upload multiple images with laravel 5.4, actually the process to upload images is done, the question is how to store them in database?
for example when gonna create a slide show of something that has several images that is uploaded, how to group them so the slide show knows those images are in one group?
can you guys help me?
Make sure you go to config/filesystems and turn 'FILESYSTEM_DRIVER' to 'public'
Model to store image paths:
class UserDocuments extends Model
{
//
protected $table = 'user_documents';
public function store($user_id, $document_link, $file_name)
{
$this->user_id = $user_id;
$this->document_link = $document_link;
$this->file_name = $file_name;
$this->save();
}
}
Code in controller to store files ('document' is file input field name)
if ($request->hasFile('document')) {
$link = $request->file('document')->store('documents');
}
$userDocuments->store(request('user_id'), url($link), $link);
To show if the images are in a group, you would have to create a database column called 'group_id' and sort them accordingly.
We have to store images into one records/row by separting commas.
class DocumentController extends Controller
{
public function store(Request $request)
{
$pid = $request->input('pid');
$input = $request->file('files');
$picture = array();
if($request->hasFile('files')) :
foreach ($images as $item):
$extension = $item->getClientOriginalName(); //image extension
$name = time() . '.' .$extension; //image name
$item->move('uploads/documents/', $name); //move to destination
$arr[] = $name; //store image into array
endforeach;
$picture = implode(",", $arr); //Image separated by comma
else:
$picture = '';
endif;
DB::table('document')->insert(array('pid' => $pid,'image' => $picture));
Session::flash('message', 'Multiple Images are uploaded successfully');
return redirect('/multiple-image');
}
}

Categories