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

jakubjv's avatar

Laravel/livewire

Hello, i have got problem, that if in dropdwon just one record left i cant submit reservation and it writes me "available_business_hour_id": ["The available business hour id field is required."] but i have available_business_hour_id for that reservation, every available time has his own id .. so this is blade

<section id="booking">
    <div class="modal fade" id="bookingModal" tabindex="-1" aria-labelledby="bookingModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <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="">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 rounded"
                                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 rounded"
                                placeholder="Email*" required>
                        </div>
                        <div class="form-group col-12">
                            <input type="text" wire:model="phone_number" id="phone_number" @error('phone_number') @enderror name="phone_number" class="form-control rounded"
                                placeholder="700 700 700*" required>
                                @error('phone_number')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                        </div>

                        <div class="form-group col-12">
                            <label for="available_business_hour_id" class="form-label">Vyberte čas rezervace:*</label>
                            <select wire:model="available_business_hour_id" name="available_business_hour_id" id="available_business_hour_id"
                                class="form-control" id="available_business_hour_id" required>
                                @foreach ($availableTimes as $time)
                                    <option value="{{ $time->id }}">{{ $time->formattedDayAndDate }}
                                        {{ $time->getFormattedDate }} od {{ $time->from }} do {{ $time->to }}
                                    </option>
                                @endforeach
                            </select>
                        </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>
            </div>
        </div>
    </div>
</section>

this is component

<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Reservation;
use App\Models\AvailableTime;
use App\Models\AvailableBusinessHour;
use App\Http\Requests\ReservationRequest;

class Booking extends Component
{

    public $full_name;
    public $email;
    public $phone_number;

    public $available_business_hour_id;
    public $availableTimes;

    public function mount()
    {
        $this->availableTimes = AvailableBusinessHour::whereDoesntHave('reservation')->get();;

    }

    public function render()
    {
        return view('livewire.booking');
    }

    public function store()
    {
        $this->validate([
            'full_name' => 'required|string|max:250',
            'email' => 'required|email|max:250',
            'phone_number' => 'required|phone:CZ',
            'available_business_hour_id' => 'required'
        ]);

        $newReserve = Reservation::create([
            'full_name' => $this->full_name,
            'email' => $this->email,
            'phone_number' => $this->phone_number,
            'available_business_hour_id' => $this->available_business_hour_id,
        ]);
        dd($newReserve);
        $this->reset(['full_name', 'email', 'phone_number', 'available_business_hour_id']);

        return back();
    }



}

and this is model

<?php

namespace App\Models;

use App\Models\AvailableBusinessHour;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Reservation extends Model
{
    use HasFactory;

    protected $table = 'reservations';
    protected $fillable = [
       'full_name',
       'email',
       'available_business_hour_id',
       'phone_number'
    ];

    public function availableBusinessHour()
    {
        return $this->belongsTo(AvailableBusinessHour::class);
    }

}

Can anyone please give me advice where can be a problem?..I am struggling with taht for few hours..this is my startup project iam new to it..

0 likes
6 replies
jaseofspades88's avatar

Despite what you might think... The available business hour id field is required will only be displayed if you aren't actually passing the available_business_hour_id. Try putting dd($this-> available_business_hour_id); before the validator and you will most likely see null.

jakubjv's avatar

@jaseofspades88 it doesnt looks like null

Illuminate\Database\Eloquent\Collection {#672 ▼ // app\Livewire\Booking.php:24
  #items: array:1 [▼
    0 => App\Models\AvailableBusinessHour {#667 ▼
      #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" => 1
        "day" => "2023-12-14"
        "from" => "12:00:00"
        "to" => "13:00:00"
        "step" => 60
        "created_at" => "2023-12-13 15:43:15"
        "updated_at" => "2023-12-13 15:43:15"
      ]
      #original: array:7 [▼
        "id" => 1
        "day" => "2023-12-14"
        "from" => "12:00:00"
        "to" => "13:00:00"
        "step" => 60
        "created_at" => "2023-12-13 15:43:15"
        "updated_at" => "2023-12-13 15:43:15"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      +usesUniqueIds: false
      #hidden: []
      #visible: []
      #fillable: array:4 [▶]
      #guarded: []
    }
  ]
  #escapeWhenCastingToString: false
jaseofspades88's avatar

Certainly shouldn't be a collection you're receiving there either considering you're attempting to pass an integer in the component, @jakubjv. I can't read your code and make sense of what it is you're trying to achieve. You're asking for guidance about a validation error.

'Hi Laracasts forum, please give me a copy and paste answer that specifically suits what I want'

jakubjv's avatar

@jaseofspades88 i am sorry, it wasnt meant that way... i am just trying to make that, for one reservation you can have multiple different available times, and when just one available time left, submit of booking form is not working.

jaseofspades88's avatar
Level 51

Your thread @jakubjv is about validation rules, now you're talking about your form not submitting, those two, albeit linked are not the same thing. My advice, watch some beginner videos here at Laracasts, you know, the forum you're using... and they'll help you.

If you're struggling with the fundamentals, then strip your component back to the point where it was working or you're comfortable with the techniques you're using, and then add, incrementally.

Please or to participate in this conversation.