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

osamashaki's avatar

Update MySql Column value to Null

Hi guys,

I have columns in the db (from, to) which are 'date' type, they are nullable and optional, however if they already have values and I tried to update/set the value to null, they are not updating.

I have tried many ways: $obj->from = null; $obj->from = NULL; $obj->from = ' '; $obj->from = '\N'

all did not work.

Is there any other ways that can help?

Thanks

0 likes
5 replies
Tray2's avatar

Can you share the code for the whole method?

osamashaki's avatar
public function removeListingAvailablePeriod($listingId, $period)
    {
        $listing = Listing::find($listingId);
        if ($listing) {
            if($period == 2)
            {
                $listing->from_date_2 = '';
                $listing->to_date_2 = '';
                $listing->save();
                return response()->json(['success' => 'Availability period removed!'], 200);
            }
            else
            {
                $listing->from_date_3 = NULL;
                $listing->to_date_3 = NULL;
                $listing->save();
                return response()->json(['success' => 'Availability period removed!'], 200);

            }
        
        } else {
            return response()->json(['error' => 'listing not found'], 400);
        }

    }
Snapey's avatar

do these column have casts on them in the model?

Please or to participate in this conversation.