Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fredsparis's avatar

Laravel eloquent returns string for decimal

Hello,

I wonder if someone ever had this issue.

I store a number in the database as a decimal. For example: 10.2. The column in the mysql database is set as "decimal" type.

When I fetch the data using Eloquent and sending it to my vue js app, the number is sent as string. I even cast in the Model, the data to decimal but still does not work.

Do you have any idea what shoould i do?

I use Mysql 8.0 on local env

0 likes
4 replies
fredsparis's avatar

Yes, so I use axios in Vue js :

            getWorksForQuote() {
                let ref = this

                axios.get('/quotes/works', {
                    params: {
                        quote: ref.quote.id
                    }
                })
                .then(function (response) {
                    if (response.data.status == 'success') {
                        ref.works = response.data.payload
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
            },

This function calls this function in laravel


    public function getWorks()
    {
        $works = QuoteWork::where('quote_id', request('quote'))->get();

        return response()->json([
            'status' => 'success',
            'payload' => $works
        ]);
    }

This is the answer :

{ 
   "status":"success",
   "payload":[
        {"id":1,
        "quote_id":1,
            "work_id":1,
        "quantity":"10.00",
        "created_at":"2020-02-24 12:55:03",
        "updated_at":"2020-02-24 12:55:03",
        "name":"Terasse"
    ]}

I have the issue with "quantity". It should be a decimal as stated here

    protected $casts = [
        'quantity' => 'decimal:2'
    ];
fredsparis's avatar

Hi @sti3bas Thank you for your reply. I decided to work differently based on that.

1 like

Please or to participate in this conversation.