如何获取父表id并将其存储到其子laravel中

如何获取父表id并将其存储到其子laravel中,laravel,laravel-5,Laravel,Laravel 5,为了找到更好的方法,我找了很多。我已经读过了 Eloquent将尝试将子模型中的父id与id匹配 在父模型上 我有和Idea和Document表格。他们有1:M的关系。一个Idea可以有多个文档,而一个文档只能与一个Idea相关 我无法获取要存储的父表id 控制器功能: public function storeDocuments(DocumentRequest $request) { if($request->hasFile('doc_name')) { $filena

为了找到更好的方法,我找了很多。我已经读过了

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');
}
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();
    });
}
$idea_id = $idea->id; // assuming the ID field is not overriden in your schema/model
文档模型:

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');
}
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();
    });
}
$idea_id = $idea->id; // assuming the ID field is not overriden in your schema/model
创意模式:

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');
}
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();
    });
}
$idea_id = $idea->id; // assuming the ID field is not overriden in your schema/model
请解释将父表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');
}
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();
    });
}
$idea_id = $idea->id; // assuming the ID field is not overriden in your schema/model
我对这种关系很困惑。现在工作一段时间,我想不出来

创意模式:

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');
}
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();
    });
}
$idea_id = $idea->id; // assuming the ID field is not overriden in your schema/model
文档架构:

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');
}
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();
    });
}
$idea_id = $idea->id; // assuming the ID field is not overriden in your schema/model

您不需要手动设置ID,Eloquent会为您这样做:

documents()
返回具有存储相关模型的保存方法的关系:

$idea->documents()->save($document);
它甚至有一个
saveMany()
方法一次存储多个:

$idea->documents()->saveMany($documents);
更新:

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');
}
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();
    });
}
$idea_id = $idea->id; // assuming the ID field is not overriden in your schema/model
您应该将其放入存储库类中。外键假定为idea\u id,但有一秒 传递给hasMany方法的ond参数,如果已构建架构,则可以重写该参数:

public function documents() {
    $this->hasMany("\App\Document", "your_foreign_key_here");
}
相关的laravel文档如下所示

希望有帮助

更新:

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');
}
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();
    });
}
$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中重新生成查询。基本上,每个模型表示一个表,表中的每个记录表示该模型类的一个实例。要查询它们,您需要在该表上有一个主键字段,以便每个记录都由该字段标识。在Elount中默认为“id”

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

SELECT * FROM document JOIN idea ON document.idea_id = idea.id WHERE idea.id = 1
但正如我已经说过的,您不必显式地存储该值,因为雄辩可以做到这一点

请给我看一下您的文档和想法表或迁移模式好吗

更新

公共功能存储文档(DocumentRequest$request){


}

由于每个
文档
必须与
创意
相关,因此控制器功能中必须有
创意
id或型号。要么你需要在表单中选择一个想法,要么你有嵌套的资源。如果我理解正确的话。您希望将想法与文档一起保存。但是您想检索最后一个Id来添加特定想法的文档吗?我是我,对吗?我需要在哪里添加这行?在
存储文档中
?你能再解释一下吗?谢谢,教授,但我太困惑了,仍然无法得到你。主键是
Idea
表中的
id
,我对这个
$Idea
变量相关的东西很困惑。如果您能重构我的
storeDocuments
函数,那就太好了。谢谢