php - 我如何从数据库中具有单个值和数组的列中查询 laravel?

标签 php mysql arrays laravel

我正在执行一个复杂的查询,该查询从具有单个值和多个值的列中查看数据库中的数据,具体取决于添加的内容。以下是可能发送的内容的片段:

[
{
"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;
     }

}

上面的查询只返回一个只包含“tuesday”的对象,而不返回一个数组中仍然包含“tuesday”的对象。

理想情况是它只在“天数”列中显示当天的结果。当前日期是使用 Carbon::now()->format('l');

创建的

最佳答案

我不太理解您代码中的所有内容,但这里最好的方法是尝试使用 LIKE 条件。

尝试这样的事情:

->where('days', 'LIKE', '%' . $dayName . '%')

这在这里会很好用,因为每天的名字都非常不同,而且它们都不包含另一个(比如 bigbigger 会在这里引起问题)。

关于php - 我如何从数据库中具有单个值和数组的列中查询 laravel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31939964/

相关文章:

php - Yii2:如何动态添加验证规则到模型类?

php - 连接到数据库只发送一个字 iOS

mysql - 使用 phpmyadmin 的 GUI 更改字段类型与使用 alter table 会产生不同的行为

php - 为什么 array_push() 不将 pdo 查询对象作为参数

php - mysql 查询到 magento 集合或在 magento 中编写连接子查询

mysql - 在 MySQL 中查找重复记录

MySQL 数据源未显示在 Visual Studio 2015 中

Javascript array.forEach 范围?

php - 在给定属性值的对象数组中查找数组键

c - 当我从函数中更改它时,为什么 `src` 没有更改?