Remove Double Quote
I can't remove double quotes form getOriginalExtension(). Can anyone help me?!
public function insert(Request $request) {
$images = $request->file('image');
$quantities = $request->quantity;
$order_infos = array_map(null, $images, $quantities);
foreach($order_infos as $info => $key) {
$order = new Order();
$order->user_id = $request->id;
$order->quantity = $key[1];
$extension = $key[0]->getClientOriginalExtension();
dd($extension);
$fileName = Str::random(20). '.' .$extension;
$key[0]->move('storage/images/', $fileName);
$order->image = $fileName;
$order->live_id = Live::orderByDesc('id')->first()->id;
$order->save();
}
return redirect('/')->with('status', 'Your order is Done!');
}
Can you give an example of what you are getting and what you are expecting to get?
I am Getting image extensions for $extension variable. But as string. I want to remove double quote from it and $fileName variable..
dd($extension)="png"
expected Output = png
dd($fileName) = "yVHlPHOPz1iOCjeCxq0S.png"
expected Output = yVHlPHOPz1iOCjeCxq0S.png
No. It doesn't. And the Database has also the double-quotes. This is a major problem.
Can you show an image of how it looks in the database perhaps? The shown code shouldnt create any quotes
But if you need to remove them this would be the code
$fileName = str_replace('"', '', $fileName);
I used
$fileName = str_replace('"', '', $fileName);
$fileName = trim($fileName, '"');
These didn't work.
@rafidahsan Instead of your dd can you echo $extension and then die(); afterwards,
What does that output look like?
Can you show an image of how it looks in the database perhaps? The shown code shouldn't create any quotes
You can use flameshot to grab an image and upload it right away (to imgur)
https://flameshot.js.org/#/
@automica
I am Getting image extensions for $extension variable. But as string. I want to remove double quote from it and $fileName variable..
dd($extension)="png" expected Output = png
dd($fileName) = "yVHlPHOPz1iOCjeCxq0S.png" expected Output = yVHlPHOPz1iOCjeCxq0S.png
@sinnbeck
"1600532044.JPG"
This is a data for image column and I am confused too. Istucked here for weeks.
But on database data has double quotes also.
So columns like the email in the users table does not have "" around it?
Yeah. It does not have. Just image has double quote
Do you by any chance have a mutator on the Order model? A method named setImageAttribute
No I don't have,
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected $fillable = [
'image', 'quantity', 'user_id'
];
protected $casts = [
'image' => 'array',
'quantity' => 'array'
];
public function lives() {
return $this->hasMany(Live::class)->withTimeStamps();
}
}
Ah there we go. You are casting the string as an array
protected $casts = [
'image' => 'string',
'quantity' => 'integer'
];
And I would assume that quantity should be an integer?
Happy to help. Casting can be confusing :) Rule of thumb. Only cast to array if the column contains json :)
Please or to participate in this conversation.