rafidAhsan's avatar

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!');
}
0 likes
19 replies
Sinnbeck's avatar

Can you give an example of what you are getting and what you are expecting to get?

rafidAhsan's avatar

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

rafidAhsan's avatar

No. It doesn't. And the Database has also the double-quotes. This is a major problem.

Sinnbeck's avatar

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);
rafidAhsan's avatar

I used

$fileName = str_replace('"', '', $fileName);
$fileName = trim($fileName, '"');

These didn't work.

automica's avatar

@rafidahsan Instead of your dd can you echo $extension and then die(); afterwards, What does that output look like?

Sinnbeck's avatar

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/#/

rafidAhsan's avatar

@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

rafidAhsan's avatar

@sinnbeck

"1600532044.JPG" This is a data for image column and I am confused too. Istucked here for weeks.

Sinnbeck's avatar

So columns like the email in the users table does not have "" around it?

rafidAhsan's avatar

Yeah. It does not have. Just image has double quote

Sinnbeck's avatar

Do you by any chance have a mutator on the Order model? A method named setImageAttribute

rafidAhsan's avatar

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();
    }
}
Sinnbeck's avatar

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?

2 likes
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

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.