mardi 13 octobre 2015

Laravel Multiple Models Eloquent Relationships Setup?

I have 3 models

User Pick Schedule

I'm trying to do something like the following

$picksWhereGameStarted = User::find($user->id)
                                ->picks()
                                    ->where('week', $currentWeek)
                                    ->first()
                                ->schedule()
                                    ->where('gameTime', '<', Carbon::now())
                                    ->get();

This code only returns one array inside a collection. I want it to return more than 1 array if there is more than 1 result.

Can I substitute ->first() with something else that will allow me to to return more than 1 results.

If not how can I set up my models relationship to allow this to work.

My models are currently set up as follow.

User model

public function picks()
{
    return $this->hasMany('App\Pick');
}

Schedule model

public function picks()
{
    return $this->hasMany('App\Pick');
}

Pick model

public function user()
{
    return $this->belongsTo('App\User');
}

public function schedule()
{
    return $this->belongsTo('App\Schedule');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire