user13289818
user13289818

Reputation: 199

How to join tables in Laravel?

I am new to Laravel, I have a question regarding Eloquent:

This is my DB table structure:

users
- id
- user_id
- username
- name
- email
- password
- email_verified_at
- remember_tok
- created_at
- updated_at

challenges
- id
- challenge_id
- category_id
- hashtag
- title
- description
- duration
- link
- created_at
- updated_at
- user_id

user_challenges
- user_id
- challenge_id
- duration
- created_at
- updated_at

What I want to achieve is to display the user's challenges, so for example, if a user joins a challenge in the user dashboard I want to display the challenges that he joined, but also the challenges that the user has created (if he has created any);

Models\User.php

class User extends Authenticatable
{
    use Notifiable;

    public function challenges() {
        return $this->hasMany(Challenge::class, 'user_id');
    }

    public function userChallenges() {
        return $this->hasMany(UserChallenge::class, 'user_id');
    }
}

Models\Challenge.php

class Challenge extends Model
{
    public function user () {
        return $this->belongsTo(User::class, 'user_id');
    }
}

Models\UserChallenge.php

class UserChallenge extends Model
{
    public function user () {
        return $this->belongsTo(User::class, 'user_id');
    }
}

This is what I have in my Home Controller: App\Http\Controllers\HomeController.php

class HomeController extends Controller
{
    public function index()
    {
        $user_id = auth()->id();
        $user = User::find($user_id);

        return view('home')
            ->with('challenges', $user->challenges)
            ->with('userChallenges', $user->userChallenges);
    }
}

This is what I get currently for the "Joined Challenges" in My Dashboard - but how can I join the tables when returning the view so I can also get the challenge hashtag title? Like I get above under "My Challenges" section? Dashboard

UPDATE: What should I add to my database so I can count the number of users that have joined the challenge?

Upvotes: 0

Views: 250

Answers (1)

Edward Chew
Edward Chew

Reputation: 504

To get user created challenges, I believe you are already doing it correctly.

And to get user joined challenges. You almost get it right with your pivot table. You can have look on many-to-many relationship in laravel documentation.

These are the sample code to retrieve challenges created by a user and challenges joined by a user. These code should reside in your User Model, you should keep the model.

public function created_challenges()
{
    return $this->hasMany(Challenge::class,'user_id','id');
}

public function joined_challenges()
{
     return $this->belongsToMany(Challenges::class, 'user_challenges', 'user_id', 'challenges_id');
}

Once you get the relationship working right, you can simply use the collection returned from the relationship. Once you pass the $user->joined_challenges to your view, you can do as below

@foreach ($joined_challenges as $challenge)
    <p>{{$challenge->hashtag}}</p>   
    <p>{{$challenge->title}}</p>   
@endforeach

Let me know it this works, cheers! ;)

Upvotes: 1

Related Questions