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.
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..
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.