Laravel hasMany和belongsTo参数

编程入门 行业动态 更新时间:2024-10-25 14:32:01
本文介绍了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的参数感到困惑,它们包含在文档它说

I'm confused with hasMany and belongsTo parameters include, in the docs it says

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

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

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

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

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

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

hasMany foreign_key和local_key的哪个表来自?与belongs一样,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('App\Library', 'what_foreign_key_should_be_here','what_other_key_should_be_here'); }

库模型

public function stores(){ return $this->belongsTo('App\Stores', '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('App\Comment', 'foreign_key', 'local_key');参数考虑为:

To simplify the syntax, think of the return $this->hasMany('App\Comment', '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('App\Library'); }

    在后台,Laravel将自动将Store表的id列链接到Library表的store_id列.

    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('App\Library', 'store_id','id'); }

    • 一个模型标准是,单数命名的函数返回一个belongsTo,而复数函数返回一个hasMany(即$store->libraries() or $library->store()).
      • A model standard is that singularly-named functions return a belongsTo, while a plural function returns a hasMany (ie. $store->libraries() or $library->store()).

    更多推荐

    Laravel hasMany和belongsTo参数

    本文发布于:2023-10-21 17:19:17,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1514850.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:参数   Laravel   hasMany   belongsTo

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!