The issue is that the date format used in the search input is different from the date format stored in the database. The solution is to convert the search input to the database date format before performing the search. This can be done in the Livewire component's render method by using the Carbon library to parse the search input and convert it to the database date format.
Here's an updated version of the render method that performs the date conversion:
public function render()
{
$today = Carbon::today();
$searchDate = Carbon::createFromFormat('m/d/Y', $this->search)->format('Y-m-d');
$orders = Order::WhereHas('status', function ($qty){
return $qty->where('name','like',"%{$this->search}%");
})
->orWhere('number','like',"%{$this->search}%")
->orWhere('slug','like',"%{$this->search}%")
->orWhere('need_by_date', $searchDate)
->orWhere('comments','like',"%{$this->search}%")
->orderBy($this->sortField,$this->sortDirection)
->paginate($this->perPage);
return view('livewire.admin.orders.orders-index',compact('orders','today'));
}
In this updated version, we first use Carbon::createFromFormat to parse the search input as a date with the format m/d/Y. We then use format to convert this date to the database date format Y-m-d. Finally, we use this converted date in the orWhere clause for the need_by_date field in the search query.
Note that we're assuming that the database date format is Y-m-d. If it's different, you'll need to adjust the format method accordingly.
Also note that we're not using any accessors or mutators in the Order model. The getNeedByDateAttribute and setNeedByDateAttribute methods can be removed.