everybody, I need to Save image name in the avatar column in the database
and save the image in public/Storage/users My Code Here Save image link in the database any help, please.
public function handleProviderCallback($service)
{
$user = Socialite::driver($service)->user();
$FindUser = User::where('email',$user->getEmail())->first();
if($FindUser){
Auth::login($FindUser);
}
else{
$NewUser = new User;
$NewUser->email = $user->getEmail();
$NewUser->name = $user->getName();
$NewUser->avatar = $user->getAvatar();
$NewUser->password = bcrypt(123456);
$NewUser->save();
Auth::login($NewUser);
}
return redirect('/');
}
Simply fetch the data using file_get_contents function and process the retrieved data.
use File;
create a helper method to get the avatar
function getSocialAvatar($file, $path){
$fileContents = file_get_contents($file);
return File::put(public_path() . $path . $user->getId() . ".jpg", $fileContents);
}
replace $user->getAvatar()
with the helper method
getSocialAvatar($user->getAvatar(), 'path')
Related
i am trying to upload multiple images in laravel, but it seems only one image in inserted into the database and i was wondering how can i be uploading all images not just one.
here is my controller
public function addProperty(Request $request){
$data = $request->all();
$pModel = new PropertyModel();
return response()->json(["type"=>$pModel->addProperty($data,$request)]);
}
here is my view
<h3>Gallery</h3>
<div class="submit-section">
<div id="selected-files"></div>
<input type="file" name="files[]" multiple onchange="Property.onImageUploadChange(event)" id="files" >
</div>
here is my model
public function addProperty($propertyData,$request){
$db = new Database;
#session_start();
$user = $_SESSION["user"];
$propertyData["user_id"] = $user->user_id;
unset($propertyData["_token"]);
unset($propertyData["property_files"]);
$res = $db->query($db->buildQuery($propertyData, $this->tableName)) or die(false);
$lastID= $db->lastInsertID($res);
$image = $request->file("property_files");
$image->move(public_path('images'),$lastID.".jpg");
return true;
}
may this code help you!
I'm work on backend and use this function:
public static function upload_image($image, $image_folder)
{
$image_new_name = time() . '_' . $image->getClientOriginalName();
$image_path = public_path('uploads\\' . $image_folder);
$image->move($image_path, $image_new_name);
return $image_folder . '/' . $image_new_name;
}
you can create a SettingController and put it there, then in your controller receive array of images and with foreach loop save them in your server like:
foreach($request->arrayImages as $image) {
SettingController::upload_image($image, 'your folder name');
}
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);
It's my update function and it's not showing image when I echo $fileName. It's not working like it was I want to make it with the condition if there is any image then update that image otherwise leave that I tried many times but it's not working.
public function update(Request $request, Header $header)
{
if($request->hasFile('headerimg')){
$fileName = $request->file('headerimg')->getClientOriginalName();
$request->file('headerimg')->move('uploads/header',$fileName);
}
$header->title = $request->title;
$header->discription = $request->discription;
$header->keywords = $request->keywords;
$header->headerimg = $fileName;
$header->h2 = $request->h2;
$header->breadcumb = $request->breadcumb;
$header->category = $request->category;
$header->save();
return redirect()->route('dashboard.headers.index')->with('success', 'Packages Edited Successfully');
}
I guess that your problem is in your view.
Maybe you forget to use enctype="multipart/form-data" or your input name does not match with mapped controller variable.
For find the exact problem use dd($request) and dd($request->file('headerimg')) and see can you get request correct or not.
I use this code edit image and if image was not selected the old image will remain same without any error.
public function update(Request $request, Header $header)
{
$file = $request->headerimg;
$id = $request->id;
if($file == ''){
$arr['header'] = Header::select()->where($id);
$fileName = $header['headerimg'];
}
else{
$fileName = $request->file('headerimg')->getClientOriginalName();
$request->file('headerimg')->move('uploads/header',$fileName);
}
$header->title = $request->title;
$header->discription = $request->discription;
$header->keywords = $request->keywords;
$header->headerimg = $fileName;
$header->h2 = $request->h2;
$header->breadcumb = $request->breadcumb;
$header->category = $request->category;
$header->save();
return redirect()->route('dashboard.headers.index')->with('success', 'Page Edited Successfully');
}
Hy all,
I need to update the picture of a article, all it's fine for updating post but my question it's how to update the image of this post with my polymorphic relation.
i have a table Images with imageable_id imageable_type path .
in my Post controller update function i have
public function update($id)
{
$inputs = Input::all();
$rules =array(
'name'=>'required|min:5',
'description'=>'required|min:10',
);
$validation = Validator::make($inputs,$rules);
if($validation->fails()){
return Redirect::to('posts/'.Input::get('id').'/edit')->withInput()
->withErrors($validation)
->with('alert_error','Veuillez corriger les érreurs SVP');
}
if(Input::hasFile('mediafile')){
$file = Input::file('mediafile');
$name = date('Ymd') . '-' . $file->getClientOriginalName();
$file = $file->move('img/uploads/', $name);
$path = $file->getRealPath();
//take the path only at the img not totally
$pos = strpos($path,'/img/');
if ($pos !== false) {
$path = substr($path, $pos + 1);
}
$input['file'] = $path;
$post=Post::find(Input::get('id'));
$post->name = e(Input::get('name'));
$post->description = e(Input::get('description'));
$post->category_id = e(Input::get('categories'));
$post->tag_id = e(Input::get('tag'));
$post->rating = e(Input::get('rating'));
$post->content = Input::get('content');
$post->is_online = e(Input::get('online'));
$post->images->path = e(Input::get('mediafile'));
$post->save();
}
}
My model Post
public function images(){
return $this->morphMany('Image','imageable');
}
So i wish update only the path of my image in my table Image when i submit my form to show the new picture of post.
Thank's for help
The morphMany will return a collection of Images, so when you run $post->images->path you're setting the path attribute on the collection not on an image model. If a Post is only meant to have one image, change it to a morphOne or alternatively you will have to identify the image model you wish to update from the collection.
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);