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

krutsednar's avatar

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.

0 likes
7 replies
parthjani7's avatar

you have error in your syntax. Career::where($this->posting, '<=', $this->today) ->where($this->closing, '<=', $this->today)->get();

here 1st argument is column name.

where('column_name', '<=', $this->today)

1 like
Snapey's avatar

a lot of pointless variables;

$this->career = Career::whereDate('posting', '<=', today()) ->whereDate('closing', '>', today())->get();

also

-use where Date to ignore any time component

-your comparison on closing is the wrong way around -you want records posted today or before today and closing in the future?

$this->career should be plural to remind you and future you that this is a collection of records, not just one model

1 like
krutsednar's avatar

@Snapey thank you. I wanted to post records if today is between opening date and closing date. solved it :D

krutsednar's avatar
krutsednar
OP
Best Answer
Level 1

solved this by changing my render

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'));
   
}
Snapey's avatar

@krutsednar so pleased you solved it on your own and did not need us pointing out your errors

Please or to participate in this conversation.