简体   繁体   English

如何在 Laravel 中加入表?

[英]How to join tables in Laravel?

I am new to Laravel, I have a question regarding Eloquent:我是 Laravel 的新手,我有一个关于 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型号\用户.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型号\挑战.php

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

Models\UserChallenge.php型号\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这就是我家 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?更新:我应该在我的数据库中添加什么,以便计算参加挑战的用户数量?

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.您几乎可以使用 pivot 表做对。 You can have look on many-to-many relationship in laravel documentation.您可以在 laravel 文档中查看多对多关系

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.这些代码应该驻留在您的用户 Model 中,您应该保留 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$user->joined_challenges传递给视图后,您可以执行以下操作

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

Let me know it this works, cheers;让我知道这行得通,干杯; ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM