在Laravel中保存模型时,直接设置ID和设置雄辩的关联关系之间的区别? [英] Difference between setting ID directly and setting eloquent associate relation when saving a model in Laravel?

查看:335
本文介绍了在Laravel中保存模型时,直接设置ID和设置雄辩的关联关系之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表,员工员工类型



员工具有以下字段




  • id(PK)

  • employee_type_id(FK)

  • 名称



code> employee_type 具有以下字段




  • id(PK)

  • title



我雄辩的模特儿功能是



Employee

  class Employee extends Model {

public function employeeTypes(){
return $ this-> belongsTo('App \Model\EmployeeType');
}
}

EmployeeType

  class EmployeeType extends Model {
}
pre>

我不知道这是否是保持关系的正确方法。
插入时可以按照以下两种方法:



1.设置ID

  $ emp = new Employee(); 
$ emp-> employee_type_id = $ request-> type_id;
$ emp-> name = $ request-> name;
$ emp-> save();

2.设置关系

  $ EMP-> employeeTypes() - >准(EmployeeType :: findOrFail($请求 - > employee_types_id)); 
$ emp-> name = $ request-> name;
$ emp-> save();

两种方法都可以正常运行。



这两种类型的插入有什么区别?



哪个是最好的方法?

解决方案

,还有第三个:



使用 associate()只需要id

  $ emp-> employeeTypes() - > associate($ request-> employee_types_id); 






现在看看有什么优点和缺点方法是:



1。手动设置外键




  • Pro:这是最简单的方法

  • Contra:你必须明确使用外键列名称



2。使用 associate()与模型




  • Pro:关系将自动设置因此,您可以稍后使用该模型,而无需先从数据库中提取该模型。

  • Pro:外键列的名称无关紧要(只需在关系声明中定义)

  • Contra:需要额外的查询以便先获取相关模型



3。使用 associate()只是id




  • Pro:外国人的名字关键列无关紧要(只需在关系声明中定义)

  • Contra:关联ID后不会加载关系。当您访问 $ emp-> employeeTypes




$时,查询将运行b
$ b

我个人更喜欢使用 associate()(基本上所有的关系方法,即使它们并不总是必需的)。如果我已经有了我的模型,否则我只是使用id。您已经定义了与外键等的关系,所以您可以使用它。


I have two tables, employees and employee types.

employees has the following fields

  • id (PK)
  • employee_type_id (FK)
  • name

and employee_type has the following fields,

  • id (PK)
  • title

My eloquent model functions are,

Employee

  class Employee extends Model {

    public function employeeTypes() {
        return $this->belongsTo('App\Model\EmployeeType');
    }
}

EmployeeType

class EmployeeType extends Model {
}

I am not sure if this is the correct way to keep a relation. While inserting I can follow these two methods:

1.Setting ID

 $emp = new Employee();
 $emp->employee_type_id = $request->type_id;
 $emp->name = $request->name;
 $emp->save();

2.Setting relation

$emp->employeeTypes()->associate(EmployeeType::findOrFail($request->employee_types_id));
$emp->name = $request->name;
$emp->save();

Both methods are working fine.

What is the difference between these two types of insertion?

Which is best method?

解决方案

Before I compare the options, there is a third one:

Using associate() with just the id

$emp->employeeTypes()->associate($request->employee_types_id);


Now let's see what the advantages and disadvantages of the methods are:

1. Setting the foreign key manually

  • Pro: It's the simplest method
  • Contra: You have to explicitly use the foreign key column name

2. Using associate() with the model

  • Pro: The relationship will be set automatically so you can use the model later without first fetching it from the database
  • Pro: The name of the foreign key column doesn't matter (it only needs to be defined in the relationship declaration)
  • Contra: An extra query is necessary to fetch the related model first

3. Using associate() with just the id

  • Pro: The name of the foreign key column doesn't matter (it only needs to be defined in the relationship declaration)
  • Contra: The relationship won't be loaded after associating the id. A query will run when you access $emp->employeeTypes

I personally prefer to use associate() (basically all relationship methods, even if they aren't always necessary). If I already have the model I pass that, and otherwise I'll just use the id. You already have defined your relationship with foreign key etc, so you might as well use it.

这篇关于在Laravel中保存模型时,直接设置ID和设置雄辩的关联关系之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆