laravel Eloquent 查询列在另一个表中的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31332360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:55:00  来源:igfitidea点击:

Eloquent query where column is in another table

phplaravellaravel-5eloquentlaravel-4

提问by user4913694

I have two tables, postsand likes. I need to create a query, using Eloquent, that gets all posts that have been liked by a specific user_id.

我有两张桌子,postslikes。我需要使用 Eloquent创建一个查询,获取特定user_id.

In other words, it should be something like this:

换句话说,它应该是这样的:

SELECT * FROM posts p LEFT JOIN likes l ON p.id = l.post_id WHERE l.user_id = 2 ORDER BY l.created_at DESC

poststable:

posts桌子:

+----+---------+------------+-------------+
| id | user_id |  message   | created_at  |
+----+---------+------------+-------------+
|  1 |       2 | Hello!     | <SOME TIME> |
|  2 |       3 | World!     | <SOME TIME> |
|  3 |       2 | Something. | <SOME TIME> |
|  4 |       2 | Another.   | <SOME TIME> |
+----+---------+------------+-------------+

likestable:

likes桌子:

+----+---------+---------+-------------+
| id | post_id | user_id | created_at  |
+----+---------+---------+-------------+
|  1 |       1 |       2 | <SOME TIME> |
|  2 |       2 |       2 | <SOME TIME> |
|  3 |       1 |       3 | <SOME TIME> |
|  4 |       3 |       2 | <SOME TIME> |
+----+---------+---------+-------------+

Here is my Postclass:

这是我的Post课:

<?php

class Post extends Eloquent {

    protected $table = 'posts';

    public function likes()
    {
        return $this->hasMany('Like');
    }

}

And the Likeclass:

Like班级:

<?php

class Like extends Eloquent {

    protected $table = 'likes';


    public function post()
    {
        return $this->belongsTo('Post');
    }

}

How can I do this?

我怎样才能做到这一点?

回答by Nicklas Kevin Frank

This should work:

这应该有效:

$userId = //however you get the userid here.

$posts = Post::whereHas('likes', function ($q) use ($userId) {
    $q->where('user_id', $user_id);
})->get();

回答by Khan Shahrukh

You can use Laravel's DB class to perform joins on two or more tables, following is how your query will be executed in laravel:

您可以使用 Laravel 的 DB 类在两个或多个表上执行连接,以下是您的查询在 Laravel 中的执行方式:

$users = DB::table('posts')
        ->leftJoin('likes', 'posts.id', '=', 'likes.post_id')
        ->select('posts.*', 'likes.*')
        ->where('likes.user_id', '=', '2')
        ->orderBy('likes.created_at', 'desc')
        ->get();

Don't forget to use DB class on the top of your controller;

不要忘记在控制器顶部使用 DB 类;

If you want to do it with eloquent, you should do the follwing:

如果你想用 eloquent 来做,你应该做以下事情:

$result = Post::whereHas('likes', function ($q) use($user_id)
                {
                   $q->where('user_id', $user_id);
                })
                  ->orderBy('likes.created_at')
                  ->get();