简体   繁体   English

Laravel 4渴望与关系有关的加载问题

[英]Laravel 4 eager loading issues with relationship

I have two tables, items and users. 我有两个表,项目和用户。 A User can have multiple Items and an Item can be owned by one User. 一个用户可以有多个项目,而一个项目可以由一个用户拥有。

My two tables have many fields but are linked by: 我的两个表有很多字段,但通过以下方式链接:

Item
----
ownerID

User
----
id

These are the fields that link the two tables together and they are set in my database with a foreign key. 这些是将两个表链接在一起的字段,它们在我的数据库中用外键设置。

Here is a shortened version of the two models: 这是两个模型的简化版本:

class Item extends Eloquent {
    public function owner()
    {   
        return $this->belongsTo('User','id');
    }
}

class User extends Eloquent implements UserInterface, RemindableInterface {
    public function items()
    {
        return $this->hasMany('Item', 'ownerID');
    }
}

So, I want to get all of my Items and eager load the owner. 因此,我想获取所有项目并渴望加载所有者。 This is my code in my Controller: 这是我的控制器中的代码:

class ItemsController extends BaseController {
    public function all(){
        $items = Item::with('owner')->get();
        return $items;
    }
}

(I am returning $items so I can debug the output) (我返回$items以便调试输出)

However, this is what I get as my output: 但是,这就是我的输出:

[
    {
        "active":"1",
        "categoryID":"1",
        "created_at":"0000-00-00 00:00:00",
        "description":"Just a test item",
        "endDate":"2014-03-01 21:46:07",
        "id":"32",
        "name":"Test Item",
        "owner":null,
        "ownerID":"1",
        "section":"0",
        "updated_at":"0000-00-00 00:00:00"
    }
]

I don't understand why owner is null? 我不明白为什么所有者为空? User ID 1 exists in my database so why is it null? 用户ID 1存在于我的数据库中,为什么它为null?

According to your function in Item 根据您在Item的功能

public function owner()
{   
    return $this->belongsTo('User','id');
}

It indicates that, the foreign key in the items table is id which is being used for relationship with users table but you have used ownerId as the foreign key in the items table which is the primary key of users table. 它表示items表中的foreign key是用于与users表建立关系的id ,但是您已将ownerId用作items表中的foreign key ,这是users表的primary key

So, it should be 所以应该

public function owner()
{   
    // This model (child) with a local-key/field ownerId
    // in it's corresponding table(items) belongs to/relates to the
    // user model (parent) with primary-key id in corresponding (users) table
    return $this->belongsTo('User','ownerID');
}

While using belongsTo/reverse the second argument passed to the function is the referencing key (child-key/local-key) in the current table and here, items is the current table, which has the referencing key ownerId as it's local-key which relates to the id field of users table. 在使用belongsTo/reverse ,传递给该函数的第二个参数是当前表中的引用键(子键/本地键),此处的items是当前表,其引用键ownerId是它的local-key ,与users表的id字段相关。

Try this 尝试这个

class Item extends Eloquent {
    public function owner()
    {   
        return $this->belongsTo('User','ownerID');
    }
}

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

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