php Laravel hasMany 和belongsTo 参数

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

Laravel hasMany and belongsTo parameters

phpmysqllaraveleloquent

提问by Naib Sorion

I have a table store, and store has many libraries, in library I have foreign key of store store_id.

我有一个表存储,并且 store 有很多库,在库中我有 store 的外键store_id

Store table

存储表

id(PK)

Library table

图书馆表

id(PK)
store_id(FK)

I'm confused with hasManyand belongsToparameters include, in the docsit says

我很困惑与hasManybelongsTo参数包括,在文档它说

return $this->hasMany('App\Comment', 'foreign_key');

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

return $this->belongsTo('App\Post', 'foreign_key', 'other_key');

返回 $this->hasMany('App\Comment', 'foreign_key');

返回 $this->hasMany('App\Comment', 'foreign_key', 'local_key');

返回 $this->belongsTo('App\Post', 'foreign_key', 'other_key');

Which table of hasMany foreign_key and local_key came from? Same with belongsTo which table of foreign_key and other_key came from?

hasMany foreign_key 和 local_key 来自哪个表?与belongsTo 一样,foreign_key 和other_key 来自哪个表?

Store model

店铺模式

public function library(){
    return $this->hasMany('App\Library', 'what_foreign_key_should_be_here','what_other_key_should_be_here');
}

Library model

图书馆模型

public function stores(){
    return $this->belongsTo('App\Stores', 'what_foreign_key_should_be_here', 'what_other_key_should_be_here');
}

Because sometimes I change my primary key id of a table to other name like sid, so I always want to specify which is foreign key and primary key

因为有时我将表的主键 id 更改为 sid 之类的其他名称,所以我总是想指定哪个是外键和主键

回答by James

To simplify the syntax, think of the return $this->hasMany('App\Comment', 'foreign_key', 'local_key');parameters as:

为了简化语法,将return $this->hasMany('App\Comment', 'foreign_key', 'local_key');参数视为:

  1. The model you want to link to
  2. The column of the foreign table (the table you are linking to) that links back to the idcolumn of the current table (unless you are specifying the third parameter, in which case it will use that)
  3. The column of the current table that should be used - i.e if you don't want the foreign key of the other table to link to the idcolumn of the current table
  1. 您要链接到的模型
  2. 链接回id当前表列的外部表(您要链接到的表)的列(除非您指定第三个参数,在这种情况下它将使用该参数)
  3. 应该使用的当前表的列 - 即如果您不希望其他表的外键链接到id当前表的列

In your circumstance, because you have used store_idin the librariestable, you've made life easy for yourself. The below should work perfectly when defined in your Storemodel:

在你的情况下,因为你store_idlibraries桌子上使用过,你让自己的生活变得轻松。在您的Store模型中定义时,以下内容应该可以完美运行:

public function libraries()
{
    return $this->hasMany('App\Library');
}

Behind the scenes, Laravel will automatically link the idcolumn of the Storetable to the store_idcolumn of the Librarytable.

在幕后,Laravel 会自动idStore表格的store_id列链接到表格的列Library

If you wanted to explicitly define it, then you would do it like this:

如果你想明确定义它,那么你可以这样做:

public function libraries(){
    return $this->hasMany('App\Library', 'store_id','id');
}
  • A model standard is that singularly-named functions return a belongsTo, while a plural function returns a hasMany (ie. $store->libraries() or $library->store()).
  • 模型标准是单数命名的函数返回一个belongsTo,而复数函数返回一个hasMany(即$store->libraries() or $library->store())。

回答by Kenneth

Try this one. It works. Add this to your model.

试试这个。有用。将此添加到您的模型中。

Library model

图书馆模型

public function store()
    {
        return $this->belongsTo(Store::class, 'store_id', 'id');
    }

Store model

店铺模式

 public function libraries()
    {
        return $this->hasMany(Library::class);
    }

example code.

示例代码。

 $store = Store::find(1);
 dd($store->libraries);

Because in this case a store has many libraries, the Storemodel has a libraries()function. Refer to last line of James' answerfor more information on this standard.

因为在这种情况下,商店有很多库,所以Store模型有一个libraries()功能。有关此标准的更多信息,请参阅James 回答的最后一行。