【问题标题】:How to get and store parent table id into its child laravel如何获取父表ID并将其存储到其子laravel中
【发布时间】:2016-07-09 12:08:42
【问题描述】:

我已经搜索了很多以找到更好的方法。我已经在Laravel docs 上读到了

Eloquent 会尝试将子模型中的父 id 匹配到一个 id 在父模型上。

我有 Idea 和 Document 表。他们有1:M的关系。一个Idea 可以有多个文档,而一个文档只能与一个Idea 相关。

我无法获取要存储的父表 ID。

控制器功能:

public function storeDocuments(DocumentRequest $request) {

  if($request->hasFile('doc_name')) {
      $filename = $request->file('doc_name')->getClientOriginalName();
      $moveFile = $request->file('doc_name')->move('documents/', $filename);
    }
    $doc = new Document();
    $doc->doc_name = $moveFile;
    $doc->save();
}

文档模型:

public function idea() {
    return $this->belongsTo('App\Idea');
}

创意模型:

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

请说明将父表的id存入子表的过程,谢谢!

编辑:

public function storeDocuments(DocumentRequest $request) {

    if($request->hasFile('doc_name')) {
        $filename = $request->file('doc_name')->getClientOriginalName();
        $moveFile = $request->file('doc_name')->move('documents/', $filename);
    }
    $doc = new Document();
    $doc->doc_name = $moveFile;
    $idea = Idea::find(1); //how to get idea_id here???????
    $idea->documents()->save($doc);
    return back();
}

我对这种关系很困惑。现在工作了一段时间,想不通。

想法架构:

public function up()
{
    //
    Schema::create('ideas', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('idea_title');
        $table->string('idea_info', 150);
        $table->string('idea_image');
        $table->string('selection');
        $table->longText('idea_description');
        $table->string('idea_location');
        $table->integer('idea_goal')->unsigned();
        $table->integer('pledge_amount')->unsigned();
        $table->string('set_equity')->nullable();
        $table->boolean('status')->default(0);
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();
    });
}

文档架构:

public function up()
{
    //
    Schema::create('documents', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('idea_id')->unsigned();
        $table->string('doc_name');
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();
    });
}

【问题讨论】:

  • 由于每个Document 都必须与Idea 相关,因此您的控制器函数 中必须有一个Idea id 或模型可用。要么您需要在表单中选择一个想法,要么您有嵌套资源。
  • 如果我理解正确的话。您想用正确的文档保存想法。但是您想检索最后一个 ID 以添加特定想法的文档?我是对的吗?

标签: laravel laravel-5


【解决方案1】:

您无需手动设置 ID,Eloquent 会为您完成:

documents() 返回一个关系,它有一个保存方法来存储相关模型:

$idea->documents()->save($document);

它甚至有一个saveMany() 方法来一次存储多个:

$idea->documents()->saveMany($documents);

更新:

你应该把它放到你的存储库类中。 foreign_key 假定为 idea_id,但有 sec ond 参数传递给 hasMany 方法,如果您已经构建了架构,则可以覆盖该方法:

public function documents() {
    $this->hasMany("\App\Document", "your_foreign_key_here");
}

相关的 laravel 文档是 here。

希望对您有所帮助。

更新:

$idea_id = $idea->id; // assuming the ID field is not overriden in your schema/model

它只是使用idea表的主键值,与你在find方法中使用的值相同。一对多关系通过默认连接工作,并使用父主键值连接子表记录。

IDEA           DOCUMENT   
-----------   ---------------
|   id    |-->| idea_id | id |
|   1     |   |    1    |  1 |
|   2     |   |    1    |  2 |
-----------   ---------------- 

好的,所以在 SQL 中重现查询。基本上每个模型代表一个表,表中的每条记录代表该模型类的一个实例。要查询它们,您需要在该表上有一个主键字段,因此每条记录都由该字段标识。它在 eloquent 中默认为 'id'。

当我们谈到关系时,我们需要另一个字段来连接相关表。在这种特殊情况下,它将是文档表中的 idea_id 字段。当获取与 ID 为 1 的想法关联的文档时,相关文档的“idea_id”字段中将包含 1,因为它们以 id = 1 连接到该想法。 SQL 查询看起来像 sg 一样:

SELECT * FROM document JOIN idea ON document.idea_id = idea.id WHERE idea.id = 1

但正如我已经说过的,您不必显式存储该值,因为 eloquent 会这样做。

能否给我看一下你的文档和想法表或迁移模式?

更新

公共函数 storeDocuments(DocumentRequest $request) {

if($request->hasFile('doc_name')) {
    $filename = $request->file('doc_name')->getClientOriginalName();
    $moveFile = $request->file('doc_name')->move('documents/', $filename);
}
$doc = new Document();
$doc->doc_name = $moveFile;
$idea = Idea::find(1); // idea id is 1, but i guess it'll come from the route or sg like that in a real world scenario
$idea_id = $idea->id; // the idea id : in this case = 1
$idea->documents()->save($doc);
return back();

}

【讨论】:

  • 我需要在哪里添加这一行?在storeDocuments 中?你能解释一下吗?谢谢
  • 对不起,我很困惑,我仍然无法得到你。主键是id 在Idea 表中,我对这个$idea 变量相关的东西很困惑
  • 如果您能重构我的storeDocuments 函数,那就太好了。谢谢
猜你喜欢
  • 2016-05-17
  • 2019-11-04
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
相关资源
最近更新 更多