如果数据存在,laravel 连接表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27664377/
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 10:36:09  来源:igfitidea点击:

laravel join table if data exists

joinlaraveleloquent

提问by Gregor Voinov

What is the best way to join table A with table B, if table B has data and if not just give me data from table A? because if I do it in this way, and there are no photos in table B I don't get data from that row from table A.

将表 A 与表 B 连接的最佳方法是什么,如果表 B 有数据,如果不只是给我来自表 A 的数据?因为如果我这样做,并且表 BI 中没有照片,则不会从表 A 的该行中获取数据。

$data =  Category::join('photos', 'categories.cover_id', '=', 'photos.id')
    ->get(['categories.id',
           'categories.position', 
           'categories.visible', 
           'categories.created_at', 
           'categories.updated_at', 
           'categories.title', 
           'photos.filename']);
    return $data;

my idea is just to make another request to get all data from table A where categories.cover_id is 0 (without join)

我的想法只是提出另一个请求,从表 A 中获取所有数据,其中 category.cover_id 为 0(无连接)

my tables are just

我的桌子只是

table A (categories)
-------------------------------
| id | title | cover_id | ... |
-------------------------------
| 1  | lorem |    1     | ... |
-------------------------------
| 2  | ipsum |    12    | ... |
-------------------------------
| 3  | dolor |    0     | ... |
-------------------------------

table B (Photos, there is no data for dolor, because i created dolor recently in table A)
---------------------------------
| id | title |  filename  | ... |
---------------------------------
| 1  | lorem |  lorem.jpg | ... |
---------------------------------
| .. | ..... |  ...jpg    | ... |
---------------------------------
| 12 | ipsum |  ipsum.jpg | ... |
---------------------------------

回答by lukasgeiter

You should be fine by just using a leftJoin(). A normal ("inner join") will only return results from both tables. But a left joinreturns all results from the lefttable (in this case categories) and everything that exists from the other table.

只需使用leftJoin(). 正常(“内连接”)只会返回两个表的结果。但是左连接返回表中的所有结果(在本例中categories)以及其他表中存在的所有内容。

$data =  Category::leftJoin('photos', 'categories.cover_id', '=', 'photos.id')
->get(['categories.id',
       'categories.position', 
       'categories.visible', 
       'categories.created_at', 
       'categories.updated_at', 
       'categories.title', 
       'photos.filename']);

Or you could...

或者你可以...

Use the power of Eloquent

使用 Eloquent 的强大功能

You only need to define the relationship (I assume you already have a Photomodel) and this gets a lot easier

你只需要定义关系(我假设你已经有了一个Photo模型),这变得容易多了

class Category extends Eloquent {
    public function photos(){
        return $this->hasMany('Photo', 'cover_id');
    }
}

And then...

进而...

$data = Category::with('photos')->get();

And you will have the photos model nested inside the category models. Accessible like this:

您将拥有嵌套在类别模型中的照片模型。可以这样访问:

foreach($data as $category){
    foreach($category->photos as $photo){
        echo $photo->filename;
    }
}

回答by Chintan Parekh

I would rather do it this way:

我宁愿这样做:

// Just assuming a few variables for better understanding
$allCategories = Category::all();

foreach ($allCategories as $category) {
    $photo = Photo::find($category->cover_id);
    if ($photo) { // $photo!=null or isset($photo) - you can use anything
        // photo is found do the additional processing
    }
    //proceed with your normal processing
}