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

jakubjv's avatar

Why is validation not working properly?

Hello everyone, I am experiencing an issue with form validation after submission. I attempted to create two reservations simultaneously in separate browsers. Although they were not created at exactly the same time, I have configured the system such that a reservation can only have one unique available business hour ID. However, I am still able to create two reservations for the same unique available business hour ID at the same time. I cannot figure out why this is happening.

this is my component

<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Reservation;
use App\Mail\ReservationMail;
use Illuminate\Support\Facades\Mail;
use App\Models\AvailableBusinessHour;

class Booking extends Component
{
    public $full_name;
    public $email;
    public $phone_number;
    public $reservation;

    public $selectedService;
    public $available_business_hour_id;
    public $selectedBusinessHourId;
    public $verification_code;
    public $successMessage;
    public $successMessageVisible;
    public $successMessageTimeout = 3000;
    public $verificationCodeSubmitted = false;
    public $successMessageClosed = false;


    public function getAvailableBusinessHours()
    {
        return AvailableBusinessHour::whereDoesntHave('reservation')
            ->where(function ($query) {
                $query->where('day', '>', now()->format('Y-m-d'))
                    ->orWhere(function ($query) {
                        $query->where('day', '=', now()->format('Y-m-d'))
                            ->where('from', '>', now()->format('H:i'));
                    });
            })
            ->orderBy('day')
            ->orderBy('from')
            ->get();
    }



    public function render()
    {
        $this->available_business_hour_id = $this->getAvailableBusinessHours();
        return view('livewire.booking', [
            'emptyOption' => 'Vyber si čas rezervace',
        ]);
    }

    public function store()
    {
        $this->validate([
            'full_name' => 'required|string|max:250',
            'email' => 'required|email|max:250',
            'phone_number' => 'required|phone:CZ',
            'selectedService' => 'required',
            'available_business_hour_id' => 'required|unique:reservations'
        ], [
            'available_business_hour_id.unique' => 'Tento termín byl již obsazen, vyber jiný pokud je nějaký dostupný.',
            'available_business_hour_id.required' => 'Výběr času objednání je povinné pole.',
            'phone_number.phone' => 'Chybný formát telefonního čísla.',
            'selectedService.required' => 'Toto je povinné pole',
        ]);

        $selectedBusinessHour = AvailableBusinessHour::find($this->selectedBusinessHourId);


        if (
            $selectedBusinessHour->day < now()->format('Y-m-d') ||
            ($selectedBusinessHour->day == now()->format('Y-m-d') && $selectedBusinessHour->from < now()->format('H:i'))
        ) {
            $this->addError('selectedBusinessHourId', 'Tento termín již není dostupný, vyber jiný.');
            return;
        }

        $verification_code = rand(100000, 999999);

        $reservation = Reservation::create([
            'full_name' => $this->full_name,
            'email' => $this->email,
            'phone_number' => $this->phone_number,
            'service' => $this->selectedService,
            'available_business_hour_id' => $this->selectedBusinessHourId,
            'day' => $selectedBusinessHour['day'],
            'from' => $selectedBusinessHour['from'],
            'to' => $selectedBusinessHour['to'],
            'verification_code' => $verification_code
        ]);
        dd($reservation);

        session(['verification_code' => $verification_code, 'current_reservation_id' => $reservation->id]);


        $this->sendReservationConfirmationMail($reservation);


        $this->dispatch('show-verification-modal');
        $this->dispatch('update-success-message', ['closed' => $this->successMessageClosed]);


        $this->reset(['full_name', 'email', 'phone_number', 'available_business_hour_id']);
    }

    public function sendReservationConfirmationMail($reservation)
    {
        Mail::to($reservation->email)->send(new ReservationMail($reservation));
    }

    public function services()
    {
        return ["Stříhání vlasů", "Stříhání + úprava vousů", "Barvení vousů", "Junior střih", "Úprava vousů"];
    }

    public function confirmReservation()
    {
        $this->validate([
            'verification_code' => 'required|digits:6',
        ], [
            'verification_code.required' => 'Zadejte prosím verifikační kód.',
            'verification_code.digits' => 'Verifikační kód musí mít 6 čísel.',
        ]);

        $user_entered_code = $this->verification_code;

        $reservation = Reservation::where('verification_code', $user_entered_code)
            ->where('is_confirmed', false)
            ->where('id', session('current_reservation_id'))
            ->latest()
            ->first();

        if ($reservation) {
            $reservation->update(['is_confirmed' => true]);
            $this->reset(['verification_code']);
            $this->successMessage = 'Rezervace proběhla úspěšně.';
            $this->successMessageVisible = true;
            $this->dispatch('hide-verification-modal');
            $this->dispatch('update-success-message', ['closed' => $this->successMessageClosed]);
        } else {
            $this->addError('verification_code', 'Neplatný verifikační kód nebo rezervace již byla potvrzena.');
        }
    }
}

this is blade

    <div class="container">
        @if ($successMessageVisible && !$verificationCodeSubmitted)
            <div x-data="{ show: @entangle('successMessageVisible'), closed: @entangle('successMessageClosed') }" x-init="setTimeout(() => { show = false; if (!closed) Livewire.emit('refresh-view'); }, {{ $successMessageTimeout }})" x-show="show" class="alert alert-success text-center">
                {{ $successMessage }}
            </div>
        @endif
        <form wire:submit.prevent="store">
            @csrf
            <div class="col-12 mb-4">
                <h1 class="modal-title fs-5 text-center" id="bookingModalLabel">Objednej se</h1>
                <p class="text-center">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Lorem, ipsum
                    dolor sit amet consectetur adipisicing elit.</p>
            </div>
            <div class="form-group col-12">
                <input type="text" wire:model="full_name" id="full_name" name="full_name" class="form-control"
                    placeholder="Celé jméno*" required>
            </div>
            <div class="form-group col-12">
                <input type="email" wire:model="email" id="email" name="email" class="form-control"
                    placeholder="Email*" required>
            </div>
            <div class="form-group col-12">
                <input type="text" wire:model="phone_number" id="phone_number" name="phone_number"
                    class="form-control" placeholder="Tel. číslo ve formátu +420700700700" required>

                @error('phone_number')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>
            <div class="form-group col-12">
                <select wire:model="selectedService" class="form-control">
                    <option value="">Vyberte službu</option>
                    @foreach ($this->services() as $service)
                        <option value="{{ $service }}">{{ $service }}</option>
                    @endforeach
                </select>
                @error('selectedService')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <div class="form-group col-12 mt-3">
                <select wire:model="selectedBusinessHourId" class="form-control">
                    <option value="">{{ $emptyOption }}</option>
                    @foreach ($available_business_hour_id as $time)
                        <option value="{{ $time['id'] }}">{{ $time['formattedDayAndDate'] }} od {{ $time['from'] }} do
                            {{ $time['to'] }}</option>
                    @endforeach
                </select>
                @if ($errors->has('available_business_hour_id') && !$errors->has('selectedBusinessHourId'))
                <span class="text-danger">{{ $errors->first('available_business_hour_id') }}</span>
            @endif
            @if ($errors->has('selectedBusinessHourId'))
                <span class="text-danger">{{ $errors->first('selectedBusinessHourId') }}</span>
            @endif
            </div>
            <div class="form-group col-12 text-center">
                <div class="cta-btns">
                    <button type="submit" class="btn btn-service me-sm-2">Odeslat</button>
                </div>
            </div>
        </form>

    </div>

I will be glad for any advice.

0 likes
18 replies
gych's avatar

Just to clarify before I give any suggestions: Currently the validation works when you leave more time between adding the reservations but not when you add them close after each other right?

jakubjv's avatar

@gych Unfortunetly no..But it works with that

 $verification_code = rand(100000, 999999);

        $existingReservation = Reservation::where('available_business_hour_id', $this->selectedBusinessHourId)
            ->where('is_confirmed', false)
            ->first();

        if ($existingReservation) {
            $this->addError('selectedBusinessHourId', 'Tento termín již byl obsazen, vyber jiný pokud je nějaký dostupný.');
            return;
        }

        $reservation = Reservation::create([
            'full_name' => $this->full_name,
            'email' => $this->email,
            'phone_number' => $this->phone_number,
            'service' => $this->selectedService,
            'available_business_hour_id' => $this->selectedBusinessHourId,
            'day' => $selectedBusinessHour['day'],
            'from' => $selectedBusinessHour['from'],
            'to' => $selectedBusinessHour['to'],
            'verification_code' => $verification_code
        ]);

but i am not sure if is it best way how to do it

gych's avatar

Oke so In your view you use this as property name: selectedBusinessHourId In validation and database you use this: available_business_hour_id So those property names are different and that seems like the cause of the issue.

Try to change the validation rule to this:

'selectedBusinessHourId' => 'required|unique:reservations,available_business_hour_id'
jakubjv's avatar

@gych but preoblem is that in databse i am working with column 'available_business_hour_id' and after i change code according to your answer it throws SQLSTATE[42S22]: Column not found: 1054 Unknown column 'selectedBusinessHourId' in 'where clause'

gych's avatar

@jakubjv Can you show me your updated code with the changes I advised ?

jakubjv's avatar

@gych component

<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Reservation;
use App\Mail\ReservationMail;
use Illuminate\Support\Facades\Mail;
use App\Models\AvailableBusinessHour;

class Booking extends Component
{
    public $full_name;
    public $email;
    public $phone_number;
    public $reservation;

    public $selectedService;
    public $available_business_hour_id;
    public $selectedBusinessHourId;
    public $verification_code;
    public $successMessage;
    public $successMessageVisible;
    public $successMessageTimeout = 3000;
    public $verificationCodeSubmitted = false;
    public $successMessageClosed = false;


    public function getAvailableBusinessHours()
    {
        return AvailableBusinessHour::whereDoesntHave('reservation')
            ->where(function ($query) {
                $query->where('day', '>', now()->format('Y-m-d'))
                    ->orWhere(function ($query) {
                        $query->where('day', '=', now()->format('Y-m-d'))
                            ->where('from', '>', now()->format('H:i'));
                    });
            })
            ->orderBy('day')
            ->orderBy('from')
            ->get();
    }



    public function render()
    {
        $this->available_business_hour_id = $this->getAvailableBusinessHours();
        return view('livewire.booking', [
            'emptyOption' => 'Vyber si čas rezervace',
        ]);
    }

    public function store()
    {
        $this->validate([
            'full_name' => 'required|string|max:250',
            'email' => 'required|email|max:250',
            'phone_number' => 'required|phone:CZ',
            'selectedService' => 'required',
            'selectedBusinessHourId' => 'required|unique:reservations'
        ], [
            'selectedBusinessHourId.unique' => 'Tento termín byl již obsazen, vyber jiný pokud je nějaký dostupný.',
            'selectedBusinessHourId.required' => 'Výběr času objednání je povinné pole.',
            'phone_number.phone' => 'Chybný formát telefonního čísla.',
            'selectedService.required' => 'Toto je povinné pole',
        ]);

        $selectedBusinessHour = AvailableBusinessHour::find($this->selectedBusinessHourId);


        if (
            $selectedBusinessHour->day < now()->format('Y-m-d') ||
            ($selectedBusinessHour->day == now()->format('Y-m-d') && $selectedBusinessHour->from < now()->format('H:i'))
        ) {
            $this->addError('selectedBusinessHourId', 'Tento termín již není dostupný, vyber jiný.');
            return;
        }

        $verification_code = rand(100000, 999999);

        $existingReservation = Reservation::where('available_business_hour_id', $this->selectedBusinessHourId)
            ->where('is_confirmed', false)
            ->first();

        if ($existingReservation) {
            $this->addError('selectedBusinessHourId', 'Tento termín již byl obsazen, vyber jiný pokud je nějaký dostupný.');
            return;
        }

        $reservation = Reservation::create([
            'full_name' => $this->full_name,
            'email' => $this->email,
            'phone_number' => $this->phone_number,
            'service' => $this->selectedService,
            'available_business_hour_id' => $this->selectedBusinessHourId,
            'day' => $selectedBusinessHour['day'],
            'from' => $selectedBusinessHour['from'],
            'to' => $selectedBusinessHour['to'],
            'verification_code' => $verification_code
        ]);

        session(['verification_code' => $verification_code, 'current_reservation_id' => $reservation->id]);


        $this->sendReservationConfirmationMail($reservation);


        $this->dispatch('show-verification-modal');
        $this->dispatch('update-success-message', ['closed' => $this->successMessageClosed]);


        $this->reset(['full_name', 'email', 'phone_number', 'available_business_hour_id']);
    }

    public function sendReservationConfirmationMail($reservation)
    {
        Mail::to($reservation->email)->send(new ReservationMail($reservation));
    }

    public function services()
    {
        return ["Stříhání vlasů", "Stříhání + úprava vousů", "Barvení vousů", "Junior střih", "Úprava vousů"];
    }

    public function confirmReservation()
    {
        $this->validate([
            'verification_code' => 'required|digits:6',
        ], [
            'verification_code.required' => 'Zadejte prosím verifikační kód.',
            'verification_code.digits' => 'Verifikační kód musí mít 6 čísel.',
        ]);

        $user_entered_code = $this->verification_code;

        $reservation = Reservation::where('verification_code', $user_entered_code)
            ->where('is_confirmed', false)
            ->where('id', session('current_reservation_id'))
            ->latest()
            ->first();

        if ($reservation) {
            $reservation->update(['is_confirmed' => true]);
            $this->reset(['verification_code']);
            $this->successMessage = 'Rezervace proběhla úspěšně.';
            $this->successMessageVisible = true;
            $this->dispatch('hide-verification-modal');
            $this->dispatch('update-success-message', ['closed' => $this->successMessageClosed]);
        } else {
            $this->addError('verification_code', 'Neplatný verifikační kód nebo rezervace již byla potvrzena.');
        }
    }
}

and template

<section id="booking">
    <div class="container">
        @if ($successMessageVisible && !$verificationCodeSubmitted)
            <div x-data="{ show: @entangle('successMessageVisible'), closed: @entangle('successMessageClosed') }" x-init="setTimeout(() => { show = false; if (!closed) Livewire.emit('refresh-view'); }, {{ $successMessageTimeout }})" x-show="show" class="alert alert-success text-center">
                {{ $successMessage }}
            </div>
        @endif
        <form wire:submit.prevent="store">
            @csrf
            <div class="col-12 mb-4">
                <h1 class="modal-title fs-5 text-center" id="bookingModalLabel">Objednej se</h1>
                <p class="text-center">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Lorem, ipsum
                    dolor sit amet consectetur adipisicing elit.</p>
            </div>
            <div class="form-group col-12">
                <input type="text" wire:model="full_name" id="full_name" name="full_name" class="form-control"
                    placeholder="Celé jméno*" required>
            </div>
            <div class="form-group col-12">
                <input type="email" wire:model="email" id="email" name="email" class="form-control"
                    placeholder="Email*" required>
            </div>
            <div class="form-group col-12">
                <input type="text" wire:model="phone_number" id="phone_number" name="phone_number"
                    class="form-control" placeholder="Tel. číslo ve formátu +420700700700" required>

                @error('phone_number')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>
            <div class="form-group col-12">
                <select wire:model="selectedService" class="form-control">
                    <option value="">Vyberte službu</option>
                    @foreach ($this->services() as $service)
                        <option value="{{ $service }}">{{ $service }}</option>
                    @endforeach
                </select>
                @error('selectedService')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <div class="form-group col-12 mt-3">
                <select wire:model="selectedBusinessHourId" class="form-control">
                    <option value="">{{ $emptyOption }}</option>
                    @foreach ($available_business_hour_id as $time)
                        <option value="{{ $time['id'] }}">{{ $time['formattedDayAndDate'] }} od {{ $time['from'] }} do
                            {{ $time['to'] }}</option>
                    @endforeach
                </select>
                @if ($errors->has('available_business_hour_id') && !$errors->has('selectedBusinessHourId'))
                    <span class="text-danger text-center">{{ $errors->first('available_business_hour_id') }}</span>
                @endif
                @if ($errors->has('selectedBusinessHourId'))
                    <span class="text-danger text-center">{{ $errors->first('selectedBusinessHourId') }}</span>
                @endif
            </div>
            <div class="form-group col-12 text-center">
                <div class="cta-btns">
                    <button type="submit" class="btn btn-service me-sm-2">Odeslat</button>
                </div>
            </div>
        </form>

    </div>
jakubjv's avatar

@gych wha if i setup in migration that these columns cannot be null?

 Schema::create('available_business_hour', function (Blueprint $table) {
            $table->id();
            $table->date('day'); //earlier string
            $table->time('from');
            $table->time('to');
            $table->unsignedInteger('step')->default(60);
            $table->timestamps();

        });
gych's avatar

In this part of your code you have to add available_business_hour_id at the end

'selectedBusinessHourId' => 'required|unique:reservations,available_business_hour_id'

Without that it looks for the db column with the property name from the validation: selectedBusinessHourId

jakubjv's avatar

@gych now i am getting this Trying to access array offset on value of type null

on that line 'day' => $selectedBusinessHour['day'], in reservation create... if i ahev select empty,,and users can stil ldo reservation for one unique time

gych's avatar

@jakubjv You get this error when $selectedBusinessHour is null and not found when you use

$selectedBusinessHour = AvailableBusinessHour::find($this->selectedBusinessHourId);

You can test that by adding dd($selectedBusinessHour); under that line like this

$selectedBusinessHour = AvailableBusinessHour::find($this->selectedBusinessHourId);
dd($selectedBusinessHour);

You can also add $this->selectedBusinessHourId to your dump and die to see which value this property has.

$selectedBusinessHour = AvailableBusinessHour::find($this->selectedBusinessHourId);
dd($this->selectedBusinessHourId, $selectedBusinessHour);
jakubjv's avatar

@gych with this refactored code its working now

<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Reservation;
use App\Mail\ReservationMail;
use Illuminate\Support\Facades\Mail;
use App\Models\AvailableBusinessHour;

class Booking extends Component
{
    public $full_name;
    public $email;
    public $phone_number;
    public $reservation;

    public $selectedService;
    public $available_business_hour_id;
    public $selectedBusinessHourId;
    public $verification_code;
    public $successMessage;
    public $successMessageVisible;
    public $successMessageTimeout = 3000;
    public $verificationCodeSubmitted = false;
    public $successMessageClosed = false;


    public function getAvailableBusinessHours()
    {
        return AvailableBusinessHour::whereDoesntHave('reservation')
            ->where(function ($query) {
                $query->where('day', '>', now()->format('Y-m-d'))
                    ->orWhere(function ($query) {
                        $query->where('day', '=', now()->format('Y-m-d'))
                            ->where('from', '>', now()->format('H:i'));
                    });
            })
            ->orderBy('day')
            ->orderBy('from')
            ->get();
    }



    public function render()
    {
        $this->available_business_hour_id = $this->getAvailableBusinessHours();
        return view('livewire.booking', [
            'emptyOption' => 'Vyber si čas rezervace',
        ]);
    }

    public function store()
    {
        $this->validate([
            'full_name' => 'required|string|max:250',
            'email' => 'required|email|max:250',
            'phone_number' => 'required|phone:CZ',
            'selectedService' => 'required',
            'selectedBusinessHourId' => 'required|unique:reservations,available_business_hour_id'
        ], [
            'selectedBusinessHourId.unique' => 'Tento termín byl již obsazen, vyber jiný pokud je nějaký dostupný.',
            'selectedBusinessHourId.required' => 'Výběr času objednání je povinné pole.',
            'phone_number.phone' => 'Chybný formát telefonního čísla.',
            'selectedService.required' => 'Toto je povinné pole',
        ]);

        $selectedBusinessHour = AvailableBusinessHour::find($this->selectedBusinessHourId);

        // dd( $selectedBusinessHour);
        if ($this->selectedBusinessHourId) {
            $selectedBusinessHour = AvailableBusinessHour::find($this->selectedBusinessHourId);

            if (
                $selectedBusinessHour &&
                $selectedBusinessHour->day &&
                ($selectedBusinessHour->day < now()->format('Y-m-d') ||
                    ($selectedBusinessHour->day == now()->format('Y-m-d') && $selectedBusinessHour->from < now()->format('H:i')))
            ) {
                $this->addError('selectedBusinessHourId', 'Tento termín již není dostupný, vyberte jiný.');
                return;
            }
        }

        $verification_code = rand(100000, 999999);

        $existingReservation = Reservation::where('available_business_hour_id', $this->selectedBusinessHourId)
            ->where('is_confirmed', false)
            ->first();

        if ($existingReservation) {
            $this->addError('selectedBusinessHourId', 'Tento termín již byl obsazen, vyber jiný pokud je nějaký dostupný.');
            return;
        }

        $reservation = Reservation::create([
            'full_name' => $this->full_name,
            'email' => $this->email,
            'phone_number' => $this->phone_number,
            'service' => $this->selectedService,
            'available_business_hour_id' => $this->selectedBusinessHourId,
            'day' => $selectedBusinessHour ? $selectedBusinessHour['day'] : null,
            'from' => $selectedBusinessHour ? $selectedBusinessHour['from'] : null,
            'to' => $selectedBusinessHour ? $selectedBusinessHour['to'] : null,
            'verification_code' => $verification_code
        ]);

        session(['verification_code' => $verification_code, 'current_reservation_id' => $reservation->id]);


        $this->sendReservationConfirmationMail($reservation);


        $this->dispatch('show-verification-modal');
        $this->dispatch('update-success-message', ['closed' => $this->successMessageClosed]);


        $this->reset(['full_name', 'email', 'phone_number', 'available_business_hour_id']);
    }

    public function sendReservationConfirmationMail($reservation)
    {
        Mail::to($reservation->email)->send(new ReservationMail($reservation));
    }

    public function services()
    {
        return ["Stříhání vlasů", "Stříhání + úprava vousů", "Barvení vousů", "Junior střih", "Úprava vousů"];
    }

    public function confirmReservation()
    {
        $this->validate([
            'verification_code' => 'required|digits:6',
        ], [
            'verification_code.required' => 'Zadejte prosím verifikační kód.',
            'verification_code.digits' => 'Verifikační kód musí mít 6 čísel.',
        ]);

        $user_entered_code = $this->verification_code;

        $reservation = Reservation::where('verification_code', $user_entered_code)
            ->where('is_confirmed', false)
            ->where('id', session('current_reservation_id'))
            ->latest()
            ->first();

        if ($reservation) {
            $reservation->update(['is_confirmed' => true]);
            $this->reset(['verification_code']);
            $this->successMessage = 'Rezervace proběhla úspěšně.';
            $this->successMessageVisible = true;
            $this->dispatch('hide-verification-modal');
            $this->dispatch('update-success-message', ['closed' => $this->successMessageClosed]);
        } else {
            $this->addError('verification_code', 'Neplatný verifikační kód nebo rezervace již byla potvrzena.');
        }
    }
}

gych's avatar

@jakubjv Nice that it works but be cautious because by adding these statements for selectedBusinessHour to the values for day, from and to theses values will propably mostly be stored as null.

'day' => $selectedBusinessHour ? $selectedBusinessHour['day'] : null,
 'from' => $selectedBusinessHour ? $selectedBusinessHour['from'] : null,
 'to' => $selectedBusinessHour ? $selectedBusinessHour['to'] : null,

Did you debug it with dd(); like I mentioned in my previous reply, if so what was the result of that? Make sure that the selectedBusinessHourId exists in the table of AvailableBusinessHour.

jakubjv's avatar

@gych It will not store them, cause user always must select some time for reservation, i did try it now and it doestn store reservation without selected time even with that and if I add dd on this $selectedBusinessHour = AvailableBusinessHour::find($this->selectedBusinessHourId); dd($this->selectedBusinessHourId, $selectedBusinessHour);

it return this

"11" // app\Livewire\Booking.php:71
App\Models\AvailableBusinessHour {#1983 ▼ // app\Livewire\Booking.php:71
  #connection: "mysql"
  #table: "available_business_hour"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:7 [▼
    "id" => 11
    "day" => "2024-01-19"
    "from" => "14:00:00"
    "to" => "15:00:00"
    "step" => 60
    "created_at" => "2024-01-18 18:03:55"
    "updated_at" => "2024-01-18 18:03:55"
  ]
  #original: array:7 [▼
    "id" => 11
    "day" => "2024-01-19"
    "from" => "14:00:00"
    "to" => "15:00:00"
    "step" => 60
    "created_at" => "2024-01-18 18:03:55"
    "updated_at" => "2024-01-18 18:03:55"
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  +usesUniqueIds: false
  #hidden: []
  #visible: []
  #fillable: array:4 [▼
    0 => "day"
    1 => "from"
    2 => "to"
    3 => "step"
  ]
  #guarded: []
}
gych's avatar
gych
Best Answer
Level 29

@jakubjv Ok I see that the value of $this->selectedBusinessHourId is a string, but it should be an integer because the id in the db has type integer.

You can remove the quotes around the option value, currently it the quotes around the value make it a string value:

<option value="{{ $time['id'] }}">

But you can use this without quotes so it will be returned as an integer to the back end

<option value={{ $time['id'] }}>

Like this:

<option value={{ $time['id'] }}>{{ $time['formattedDayAndDate'] }} od {{ $time['from'] }} do
	{{ $time['to'] }}
</option>
1 like
jakubjv's avatar

@gych omg, this is perfect,, i thought problem was on backend...iam idiot :D .. thank you very much, this is better solution than mines with ternary operator

1 like
enoch91's avatar

@jakubjv I think what you need is Laravel's database transactions and locking mechanisms. wrap your entire store method process in a DB::transaction like so

public function store()
{
    \DB::transaction(function () {

        $this->validate([
            'full_name' => 'required|string|max:250',
            'email' => 'required|email|max:250',
            'phone_number' => 'required|phone:CZ',
            'selectedService' => 'required',
            'available_business_hour_id' => 'required|unique:reservations'
        ], [
            'available_business_hour_id.unique' => 'Tento termín byl již obsazen, vyber jiný pokud je nějaký dostupný.',
            'available_business_hour_id.required' => 'Výběr času objednání je povinné pole.',
            'phone_number.phone' => 'Chybný formát telefonního čísla.',
            'selectedService.required' => 'Toto je povinné pole',
        ]);

        // Remaining code...
    });
}
1 like

Please or to participate in this conversation.