如何使用 Laravel 4 Eloquent hasMany 关系 select 记录不在日志 table 上?

How to select records not on log table using Laravel 4 Eloquent hasMany relation?

我的 table 架构

+rules 
 -id
 -title
 -description

+log
 -id
 -rules_id
 -date  MYSQL(YYYY-mm-dd)

我每天执行规则并更新日志 table。如何获取今天未执行的规则?

规则模块

public function log()
{
    return $this->hasMany('Log');
}

使用whereDoesntHave:

$rules = Rules::whereDoesntHave('log', function($q){
    $q->where('date', Carbon::now()->toDateString());
})->get();

whereDoesntHave 最近才添加(2014 年 12 月)您可能需要更新 Laravel。如果你不能使用这个:

$rules = Rules::whereHas('log', function($q){
    $q->where('date', Carbon::now()->toDateString());
}, '<', 1)->get();

如果您不太了解 @lukasgeiter

提到的 Carbon::now(),您可以使用没有 eloquent 的简单查询
$today = DATE('Y-m-d');
$rules = DB::table('rules')
        ->select('rules.id','rules.title','rules.description','log.date')
        ->join('log', 'rules.id', '=', 'rules.rules_id')
        ->where('DATE(log.date)', '!=', $today)
        ->first();