Search code examples
laravellaravel-livewire

Laravel/Livewire display data on table if today is between 2 dates


I'm trying to populate my table with data if today is between posting date and closing date.

public function mount(Career $career)
    {
        $this->today = Carbon::now();
        $this->posting = Carbon::parse($career->posting_date);
        $this->closing = Carbon::parse($career->closing_date);
        $this->career = Career::where($this->posting, '<=', $this->today) ->where($this->closing, '<=', $this->today)->get();
        $this->sortBy            = 'id';
        $this->sortDirection     = 'asc';
        $this->perPage           = 100;
        $this->paginationOptions = config('project.pagination.options');
        $this->orderable         = (new Career())->orderable;
        
    }

I'm getting this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column '2022-05-06 01:10:12' in 'where clause'

also if I try to display $today, $posting and $closing on my blade, they all show date today.


Solution

  • I solved it by editing my render and putting >whereDate.

    public function render()
    {   
        $query = Career::advancedFilter([
            's'               => $this->search ?: null,
            'order_column'    => $this->sortBy,
            'order_direction' => $this->sortDirection,])
            ->whereDate('posting_date', '<=', Carbon::now())
            ->whereDate('closing_date', '>=', Carbon::now());
    
        $careers = $query->paginate($this->perPage);
    
        return view('livewire.career.vacant', compact('careers', 'query'));  
    }