0

I'm using Laravel 5.8 to build a babysitting site. I have 4 tables with different relationships as below:

please see this image

The relationships are:

Babysitter->hasMany(session)
Sessions->hasOne(Review)
Sessions->hasOne(Kids)
Sessions->hasOne(Babysitter)
Sessions->hasOne(Parent)

I want to achieve 2 things:

First one

I want to show this result when listing all babysitters. I'm showing this information for each babysitter:

plsease see this image

See here what I couldn't achieve

plsease see this image

This is my code

Sitters::where('Status', 'active')->where('Verified', 1)->get();

Second one

Also, I've tried to show kids name with parent review as shown here:

plsease see this image

This is what i'm using

Sessions::select('Reviews.*', 'Sessions.Parent_id')->join('Reviews', 'Reviews.Session_id', '=', 'Sessions.id')->with('owner')->where('Trainer_id', session('user')->Id)->where('Status', '=', 'complete')->with('owner')->orderBy('Sessions.id', 'DESC')->get();

Here is Session.php Model

public function owner(){
    return $this->belongsTo('App\Models\Parents', 'Parent_id');
}
5
  • The other way of HasMany relation is belongsTo not HasOne. Sessions->hasOne(Review), Sessions->belongsTo(Kids), Sessions->belongsTo(Babysitter), Sessions->belongsTo(Parent). Try see if this will allow you to query build using the relations. Commented Aug 23, 2019 at 14:09
  • Also Sessions and Reviews can be merged into the same table with nullable review's fields. but that's just a preference Commented Aug 23, 2019 at 14:10
  • I used both (BelongsTo) and (hasOne) without any luck L(, 5 days now and it seems that I haven't reached to a point. that's why I posted here :( @N69S Commented Aug 23, 2019 at 14:25
  • Is it sesstion or session? Are those typos? Plus, it's better including relevant parts of your code here than putting inline image links. The links are very distracting. Commented Aug 23, 2019 at 14:49
  • it is session, my bad :) ... I put my code, and the images are to show you what I'm looking for as a final design. @udo-e Commented Aug 23, 2019 at 17:06

2 Answers 2

1

As discussed change the relations:

Babysitter->hasMany(sesstion)
Sessions->hasOne(Review)
Sessions->belongsTo(Kids)
Sessions->belongsTo(Babysitter)
Sessions->belongsTo(Parent)

First one

in Babysitter.php declare the following attributes

class Babysitter extends Model
{
    public function reviews()
    {
        $this->hasManyThrough(Review::class, Session::class);
    }

    public function getAverageReviewAttribute()
    {
        return $this->reviews()->avg('Rating');
    }
}

Then you just need to call it on the model instance.

$babysitter = Babysitter::first();
return $babysitter->average_review;

Second one

Just use the relation

$babysitter = BabySitter::with(['sessions' => public function ($session) {
        $session->with(['review','parent','kids']);
    })->where('trainer_id', '=', session('user')->Id) //did not understand this condition
    ->first();

This assumes you have parent, kids and review relation declared on Session::class. (change the names if needed)

Sign up to request clarification or add additional context in comments.

2 Comments

What is the type of "declared relations" inside Session::class , hasMany or BelongsTo or ... ? @n69s
The Session::class relations are the first thing in my answer
0

After a few days of searching & testing, this is what worked for me:

Inside (Sitters) Model, put this relation

public function sessions()
{
    return $this->hasMany(Sessions::class, 'sitter_id')
        ->withCount('reviews')
        ->withCount(['reviews as review_avg' => function($query){
            $query->select(DB::raw('AVG(Rating)'));
        }]);
}

Also, inside (Sessions) Model, put this relation

public function reviews()
{
    return $this->hasOne(Reviews::class, 'Session_id');
}

Now you query like this

return $sitters = Sitters::with('sessions')->get();

I hope this can help someone :)

1 Comment

So you are averaging the rating by session and not by Babysitter ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.