我正在执行一个复杂的查询,该查询从具有单个和多个值的列查看数据库中的数据,具体取决于添加的内容。以下是可能发送的内容的片段:
[
{
"id": 1,
"offer_title": "",
"offer headline": "",
"offer_subheader": "small subheader",
"image": "2015-08-10-09-2.png",
"thumbnail": "2015-08-10-09-1.png",
"offer_terms": "the terms",
"venue_name": "the venue number 2",
"venue_headline": "the headline",
"venue_description": "the venue is awesome",
"venue_phone_number": "00445676833",
"venue_website": "site.co.uk",
"venue_latitude": 999999.99,
"venue_longitude": -999999.99,
"offer_when": "tomorrow",
"days": "tuesday",
"featured_date": "",
"offer_end_time": "08:50",
"offer_start_time": "08:50",
"created_at": "2015-08-10 09:50:50",
"updated_at": "2015-08-11 07:50:59",
"deleted_at": null,
"offer_headline": "large header",
"venue_address": "55 road",
"offer_start_date": "08/11/2015",
"offer_end_date": "08/11/2015"
},
{
"id": 2,
"offer_title": "",
"offer headline": "",
"offer_subheader": "the subheader",
"image": "2015-08-11-09-logotype.png",
"thumbnail": "2015-08-11-09-logotype.png",
"offer_terms": "terms",
"venue_name": "a name",
"venue_headline": "this headline",
"venue_description": "the venue is cool",
"venue_phone_number": "00445676833",
"venue_website": "anothersite.co.uk",
"venue_latitude": 999999.99,
"venue_longitude": -999999.99,
"offer_when": "tomorrow",
"days": "monday,tuesday,wednesday",
"featured_date": "",
"offer_end_time": "20:29",
"offer_start_time": "15:29",
"created_at": "2015-08-11 09:31:12",
"updated_at": "2015-08-11 09:31:12",
"deleted_at": null,
"offer_headline": "the header",
"venue_address": "55 road",
"offer_start_date": "08/11/2015",
"offer_end_date": "08/11/2015"
}
]
正如您在每个对象中看到的那样,有一个名为days
的列,每个列都有一个列表或一个值。我想要实现的是通过今天的当天查询数据库,并在这里显示与今天当天匹配的项目,例如,星期二,所以所有优惠都在周二'或者星期二,星期三,星期五'应该在今天的星期二出现,如果他们不会再出现在另一天。这是我到目前为止所尝试过的,它只给了我单值days
,结果不是带有数组的那个:
public function getOffers(Offer $offer)
{
$mytime = Carbon::now();
$today = $mytime->format('m/d/Y');
$day = $mytime->format('l');
$thisDay = strtolower($day);
$offerWhen = $offer['offer_when'];
foreach(Offer::all() as $offerObject){
$offerObject->update(['offer_when' => 'tomorrow']);
$the_days = $offerObject['days'];
$offersAll = $offerObject::whereIn('days', array($the_days) )->orWhere('days', '=', array($the_days))->get();
return $offersAll;
}
}
以上查询仅返回一个星期二'在它,而不是一个仍然有星期二'在它。
理想的情况是,它只会在“天”中显示当天的结果。柱。当前日期是使用Carbon::now()->format('l');
答案 0 :(得分:1)
我并不真正了解代码中的所有内容,但最好的方法是尝试使用LIKE条件。
尝试类似的东西:
->where('days', 'LIKE', '%' . $dayName . '%')
这在这里工作得很好,因为全天的名字都非常不同,而且没有一个包含其他名称(例如big
,bigger
,这会导致问题)。