i'm trying to store image to my DB using laravel with flutter
this is my flutter code:
and this is what i get in file.path:
here my laravel code:
public function store(Request $request){
dd(request->all);
$input =$request->json()->all();
$doctor = new AcceptDoctor();
echo "before if";
if ($request->hasFile('file')) {
echo "inside if";
$path = $request->file('file')->store('images');
$doctor-> signature_path = $path;
}
$doctor->save();
return $doctor;
}
this is the output:
dd output:
the if statement not working and i try to echo the signature_path is null
i try the same code using html it's work
what should i do?
if ($request->hasFile('file')) {
$path = $request->file('signature_path')->store('images');
$doctor-> signature_path = $path;
}
This piece of code seems conflicting, first you check if file is available, and then you try to access signature_path. My guess, according to your error message, it should be:
if ($request->hasFile('file')) {
$path = $request->file('file')->store('images');
$doctor-> signature_path = $path;
}
if this doesn't work, paste the code:
dd($request->all());
on the very first line of store method and let us know
Related
I'm retrieving a users profile picture with:
Auth::user()->photo->thumbnail
In the user model I can see this:
public function getPhotoAttribute()
{
$file = $this->getMedia('photo')->last();
if ($file) {
$file->url = $file->getUrl();
$file->thumbnail = $file->getUrl('thumb');
$file->preview = $file->getUrl('preview');
}
return $file;
}
However, when the user didn't upload a picture, I want to use a default picture. I have honestly no idea how. I've tried changing the function to this:
$file = $this->getMedia('photo')->last();
if (!$file) {
$file = asset("public/default.jpg");
}
$file->url = $file->getUrl();
$file->thumbnail = $file->getUrl('thumb');
$file->preview = $file->getUrl('preview');
return $file;
But that gives "Call to a member function getUrl() on string".
Anyone could help me on the way!
Thanks
Laravel asset() function returns the URL as string. Can see here: https://laravel.com/docs/8.x/helpers#method-asset
You can do something like this...
Just return your default image paths with the help of asset() function if the file doesn't exists.
public function getPhotoAttribute()
{
$file = $this->getMedia('photo')->last();
if ($file) {
$file->url = $file->getUrl();
$file->thumbnail = $file->getUrl('thumb');
$file->preview = $file->getUrl('preview');
} else {
$file->url = asset("public/default.jpg");
$file->thumbnail = asset("public/default-thumb.jpg");
$file->preview = asset("public/default-preview.jpg");
}
return $file;
}
I want to update user image but it's not getting updated.
I tried with $request->hasFile('image') but when I return these statement it always returns false
here is my controller
if($request->hasFile('image'))
{
$file = $request->file('image');
$file->move(public_path().'/image/',$file->getClientOriginalName());
$file_name = $file->getClientOriginalName();
DB::table('settings')
->where('id', $id)
->update(['logo' => $file_name]);
}
when I write
return response()->json($request->hasFile('image'));
it will return always false
try use this one instead
if ($request->file('image')) {
// your image upload code here
}
Hi I'm working on update products form of e-commerce site but when I try to edit details then it shows error "Undefined variable: fileName" and error line is:
Product::where(['id'=>$id])->
update(['product_name'=>$data['product_name'],
'product_code'=>$data['product_ code'],
'product_color'=>$data['product_color'],
'description'=>$data['description'],
'price'=>$data['price'],'image'=>$fileName]);
return redirect()->back()->with('flash_message_success','Product
updated successfully!');
or when i try to update image only then its error is: "Creating default object from empty value", or error line is:
$product->image = $filename;
this is code of ProductsController:
public function editProduct(Request $request, $id=null){
if($request->isMethod('post')){
$data = $request->all();
//echo "<pre>"; print_r($data); die;
if($request->hasFile('image')){
$image_tmp = Input::file('image');
if($image_tmp->isValid()){
$extension = $image_tmp->getClientOriginalExtension();
$filename = rand(111,99999).'.'.$extension;
$large_image_path =
'images/backend_images/products/large/'.$filename;
$medium_image_path =
'images/backend_images/products/medium/'.$filename;
$small_image_path =
'images/backend_images/products/small/'.$filename;
// Resize Images
Image::make($image_tmp)->save($large_image_path);
Image::make($image_tmp)->resize(600,600)->save($medium_image_path);
Image::make($image_tmp)->resize(300,300)->save($small_image_path);
// Store image name in products table
$product->image = $filename;
}
}
if(empty($data['description'])){
$data['description'] = '';
}
Product::where(['id'=>$id])-
>update(['product_name'=>$data['product_name'],
'product_code'=>$data['product_code'],
'product_color'=>$data['product_color'],
'description'=>$data['description'],
'price'=>$data['price'],'image'=>$fileName]);
return redirect()->back()->with('flash_message_success','Product
updated successfully!');
}
//Get product details
$productDetails = Product::where(['id'=>$id])->first();
return view('admin.products.edit_product')-
>with(compact('productDetails'));
}
"Undefined variable: fileName"
There's a typo in your variable. Change $fileName to $filename.
i guess you need to do something like this
$product=new Product();
$product->image = $filename;
also where did you define $fileName in your code?
I am working on uploading a video locally and then use it locally to upload to s3. It successfully uploads to s3 but I get this error:
Image source not readable
/var/www/yt/vendor/intervention/image/src/Intervention/Image/AbstractDecoder
I found this error when I was trying to delete the image after I uploaded it to s3. I think it is the reason the delete does not work. How do I fix the error?
public function avatar(Request $request)
{
$imageData = $request->get('image');
$img = Image::make($request->get('image'))->fit(300)->encode('jpg');
// calculate md5 hash of encoded image
$hash = md5($img->__toString());
// use hash as a name
$path = "avatars/{$hash}.jpg";
// save it locally
$test = $img->save(storage_path($path));
// $url = "/images/{$hash}.jpg"
$url_local = $path;
$path = Storage::putFile('avatars', new File(storage_path($url_local)));
if($path){
//TODO make into event
Storage::disk('local')->delete(storage_path($url_local));
// \Debugbar::error($test);
}
$user = User::find(Auth::id());
$user->avatar_url = $path;
if ($user->save()) {
return response()->json(['avatar' => $user->gen_avatar()]);
}
}
I have a button that runs the below code. The csv file writes fine, but for some reason it is not downloading. Nothing happens, no errors. I have used the force download before and it has worked fine.
Any ideas?
$csvContent = $this->dbutil->csv_from_result($query);
$date = date('U');
$filename = date('Y-m-d-Gis', $date);
if (!write_file("./reports/$filename.csv", $csvContent)) {
echo 'Unable to write the file';
} else {
echo 'File written!';
$data = file_get_contents("reports/$filename.csv");
force_download($filename, $data);
}
You should try $data = file_get_contents("./reports/$filename.csv"); instead ;)