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

hjortur17's avatar

Pass computed prop to a post method

I'm trying to create a post method for my booking system. I'm using a computed prop to calculate the number of dates, which I'm using in a few places. When I post this form I get a General error: 1364 Field 'numberOfDays' doesn't have a default value (SQL: insert into `booki into my .log file. Any idea how to pass this prop into this post request.

Here is my post method:

createBooking() {
                axios.post('/api/booking/create', { 
                    carNumber: this.booking.carNumber,
                    carSize: this.booking.carSize,
                    carType: this.booking.carType,
                    carSubtype: this.booking.carSubtype,
                    carColor: this.booking.carColor,

                    name: this.booking.name,
                    socialId: this.booking.socialId,
                    email: this.booking.email,
                    phone: this.booking.phone,

                    dropOffDate: moment(this.selectedValue.start).format('DD/MM/YYYY'),
                    pickUpDate: moment(this.selectedValue.end).format('DD/MM/YYYY'),
                    dropOffTime: this.booking.dropOffTime,
                    pickUpTime: this.booking.pickUpTime,
                    flightNumber: this.booking.flightNumber,

                    numberOfDays: this.numberOfDays,
                    priceForDays: this.priceForDays,

                    totalPrice: this.total,
                    finalPrice: 100
                })
                .catch(error => {
                    error.response.data;
                })
                .then(function (response) {})
            },

And here is the computed method:

numberOfDays: function () {
    return Math.abs(moment(this.selectedValue.start).diff(moment(this.selectedValue.end), 'days'))
},
0 likes
4 replies
click's avatar

The error you get is a mysql error, the code you show is VueJS. So you first need to figure out what the problem is.

There could be several things wrong:

  1. numberOfDays is empty in your vuejs code
  2. numberOfDays is not passed to your save function
  3. numberOfDays is passed to your save function but it's not allowed for massAssingment (did you set the fillable for example)?
hjortur17's avatar
  1. It's not empty
  2. I'm not sure what you are talking about :)
  3. I'm using protected $guarded = [];
click's avatar
click
Best Answer
Level 35

this comes down to basic debugging. How do you know it is not empty? We can't see your code. How does your save function look like?

What I mean with number 2 is how do you save your data.

YourModel::create($yourArrayOfValues);
// or 
$yourModel->numberOfDays = $yourArrayOfValues['numberOfDays']; // or request()->numberOfDays or whatever variant. 

This defines if you need to set your $fillable or not.

Other wild guesses:

  1. You have validation in place but numberOfDays is not in the validation rule.
$validatedData = request()->validate([/* rules */]);
  1. Something in your model is resetting the value?
hjortur17's avatar

I forgot to add 'numberOfDays' => request('numberOfDays') into my STORE function in BookingController. @click Thanks :)

Please or to participate in this conversation.