yn1043
yn1043

Reputation: 588

how to get the data within the period?

We have an Article Table, has articles. The article has publish_date and expire_date, which are nullable.

Article Table
 $table->date('publish_period_from')->nullable();
 $table->date('publish_period_to')->nullbale();

We want to retrieve articles, which are on published at that time. This is our query.

$query->where('publish_period_from', '<=' today())
      ->where('publish_period_to', '>=' today())

but in this case, if an article's date is "null", we can't retrieve the article data.

Do you know how to retrieve it?

converting the columns from nullable() to required is better??

we are using laravel5.5

Upvotes: 0

Views: 62

Answers (2)

George Hanson
George Hanson

Reputation: 3040

Something like this should work fine:

$query->where(function ($query) {
    $query->where('publish_period_from', '<=' today())
          ->where('publish_period_to', '>=' today())
})->orWhere(function ($query) {
    $query->whereNull('publish_period_from')
          ->whereNull('publish_period_to');
})->get();

That will return the results where it is published within that time constraint, or where both values are null.

Upvotes: 1

pierDipi
pierDipi

Reputation: 1487

You can use this method ->orWhereNull('field')

Upvotes: 0

Related Questions