Laravel hasMany and belongsTo parameters(Laravel hasMany 和belongsTo 参数)
问题描述
我有一个表存储,存储有很多库,在库中我有存储store_id
的外键.
I have a table store, and store has many libraries, in library I have foreign key of store store_id
.
存储表
id(PK)
图书馆表
id(PK)
store_id(FK)
我对 hasMany
和 belongsTo
参数感到困惑,包括在 docs 它说
I'm confused with hasMany
and belongsTo
parameters include, in the docs it says
return $this->hasMany('AppComment', 'foreign_key');
return $this->hasMany('AppComment', 'foreign_key');
return $this->hasMany('AppComment', 'foreign_key', 'local_key');
return $this->hasMany('AppComment', 'foreign_key', 'local_key');
return $this->belongsTo('AppPost', 'foreign_key', 'other_key');
return $this->belongsTo('AppPost', 'foreign_key', 'other_key');
hasMany foreign_key 和 local_key 来自哪个表?和belongsTo一样,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?
店铺模式
public function library(){
return $this->hasMany('AppLibrary', 'what_foreign_key_should_be_here','what_other_key_should_be_here');
}
图书馆模型
public function stores(){
return $this->belongsTo('AppStores', 'what_foreign_key_should_be_here', 'what_other_key_should_be_here');
}
因为有时候我把一个表的主键id改成sid这样的其他名字,所以我总是想指定哪个是外键和主键
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
推荐答案
为了简化语法,想到了 return $this->hasMany('AppComment', 'foreign_key', 'local_key');
参数为:
To simplify the syntax, think of the return $this->hasMany('AppComment', 'foreign_key', 'local_key');
parameters as:
- 您要链接到的模型
- 链接回当前表的
id
列的外部表(您要链接到的表)的列(除非您指定了第三个参数,在这种情况下它将使用那个) - 应该使用的当前表的列 - 即如果您不希望其他表的外键链接到当前表的
id
列
- The model you want to link to
- The column of the foreign table (the table you are linking to) that links back to the
id
column of the current table (unless you are specifying the third parameter, in which case it will use that) - 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
id
column of the current table
在您的情况下,因为您在 libraries
表中使用了 store_id
,您的生活变得轻松了.在您的 Store
模型中定义时,以下内容应该可以完美运行:
In your circumstance, because you have used store_id
in the libraries
table, you've made life easy for yourself. The below should work perfectly when defined in your Store
model:
public function libraries()
{
return $this->hasMany('AppLibrary');
}
在幕后,Laravel 会自动将Store
表的id
列链接到Library<的
store_id
列/code> 表格.
Behind the scenes, Laravel will automatically link the id
column of the Store
table to the store_id
column of the Library
table.
如果你想明确定义它,那么你可以这样做:
If you wanted to explicitly define it, then you would do it like this:
public function libraries(){
return $this->hasMany('AppLibrary', 'store_id','id');
}
- 模型标准是单数命名的函数返回一个belongsTo,而复数函数返回一个hasMany(即
$store->libraries() 或$library->store()
).
这篇关于Laravel hasMany 和belongsTo 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel hasMany 和belongsTo 参数
基础教程推荐
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01