我在以下三个表之间建立了关系:Card,Event和Presence(关联表)。存在我的关联表,该表包含id_card和id_event。我试图创建一个返回$ count(foreach成员的事件数)的函数。 但是我的问题是,如果所有事件的数量为零或结果为1,则返回$ count 我的控制器:
$count = DB::table('presences')
->join('cards','cards.id','presences.id_card')
->join('events','events.id','presences.id_event')
->where('presences.date','like','%event.dateEvent%')
->where('presences.qrcode','like','%cards.qrcode_membre%')
->get()
->count();
模型卡:
public function event()
{
return $this->belongsToMany('App\Event');
}
迁移:
$table->bigIncrements('id');
$table->string('nom');
$table->string('prenom');
$table->string('cin');
$table->date('dateNaissance');
$table->date('dateAffection');
$table->string('qrcode_membre')
模型事件:
public function card()
{
return $this->belongsToMany('App\Card');
}
迁移:
$table->bigIncrements('id');
$table->string('libelle');
$table->string('description');
$table->date('dateEvent');
模型存在:
$table->bigIncrements('id');
$table->date('heureEntree');
$table->date('heureSortir');
$table->date('date');
$table->string('qrcode');
$table->unsignedInteger('id_card');
$table->foreign('id_card')->references('id')->on('cards');
$table->unsignedInteger('id_event');
$table->foreign('id_event')->references('id')->on('events');