I'm almost messed up in this case where I need to fetch perfect friend status for users along with schedule I have the following structure in DB
schedule
scheduleId
userHomeCourtId
timeFrom
timeTo
userHomeCourt
userHomeCourtId
userId
homeCourtId
User
userId
firstName
email
lastName
For the above relationship for friends I have separate table that have following fields
Relation
relationId
userId
friendId
I have following relationships in model to fetch all details in model
Schedule
public function withUser()
{
return $this->hasOne(UserHomeCourt::class, 'userHomeCourtId', 'userHomeCourtId')->with('user');
}
Inside UserHomeCourt
public function user()
{
//Many to one relation between userHomeCourts and users
return $this->belongsTo(User::class, 'userId', 'userId')->with('friend');
}
User Model
public function friend()
{
return $this->hasOne(Relation::class, 'friendId', 'userId');
}
and the following query to achieve this
$getSchedule = Schedule::with(['withUser'])
->whereHas('withUser', function ($query) use ($userId) {
$query->where('userId', '!=', $userId);
})->whereHas('friend',function($query1) use ($userId){
$query1->where('friendId',$userId);
})
->whereHas('withUser', function ($query) use ($userHomeCourtId, $userId) {
$query->where('homeCourtId', '=', $userHomeCourtId);
})
->where('scheduleStatus', Constant::STATUS_0)
->where('timeFrom', '<=', $request->currentTime)
->where('timeTo', '>', $request->currentTime)
->get()->makeHidden(['withUser', 'homeCourt']);
suggest me how to get rid of this help.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire