I have three models. Sponsor, Optin and Participant. Sponsor has a one-to-many relationship with Optin and Optin has a one-to-one Participant.
Which make the models look like this:
Sponsor model
class Sponsor extends Model
{
public function optins()
{
return $this->hasMany(Optin::class);
}
}
Optin model
class Optin extends Model
{
public function sponsor()
{
return $this->belongsTo(Sponsor::class);
}
public function participant()
{
return $this->belongsTo(Participant::class);
}
}
Participant model
class Participant extends Model
{
public function optins()
{
return $this->hasMany(Optin::class);
}
public function scopeCreatedToday($query)
{
return $query->whereDate('created_at', Carbon::today());
}
}
Now in a daily scheduled task I want to iterate through all participants that where created today and that opted in to a certain Sponsor to send them like an email or something. Now I got as far as echoing the id's of the belonging participants. But the next step is to get the participant object and filter them by the right created date. But I am a little lost on how to handle this.
$sponsor = Sponsor::find(1);
$count = $sponsor->optins()->count();
echo("Count: " . $count . "\n");
$optins = $sponsor->optins()->get();
foreach($optins as $optin)
{
echo($optin->participant_id . "\n");
}
EDIT:
After rethinking the structure I figured out that Optin has a many-to-one relation with Participant.