2015-09-06 63 views
0

我想了解一些东西。我有一个有特定领域的项目表。然后我有一个文档表,它有一个项目的外键。为了证明,我有以下模式Laravel 5处理一对一的关系

Schema::create('projects', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->string('contactName')->default(''); 
    $table->string('projectName')->default(''); 
    $table->timestamps(); 
}); 

Schema::create('document', function(Blueprint $table) 
{ 
    $table->increments('id'); 
    $table->longText('documentType')->default(''); 
    $table->longText('documentContent')->default(''); 
    $table->integer('projectId')->unsigned()->default(0); 
    $table->foreign('projectId')->references('id')->on('projects')->onDelete('cascade'); 
    $table->timestamps(); 
}); 

然后我的项目模式具有以下

public function document() 
{ 
    return $this->hasOne('App\Document', 'projectId'); 
} 

我的文档模型具有以下

public function project() 
{ 
    return $this->belongsTo('App\Project', 'projectId'); 
} 

所以一个项目可以有一个文档。这是我想了解的。我的文档是在项目之后创建的。首先,我通过填写链接到项目创建功能的表单来创建一个项目。完成后,我将被带到那个项目显示页面。在节目页面上,我有一个表格来完成文档

{!! Form::model(new App\Document, 
    [ 
     'class'=>'form-horizontal', 
     'route' => ['document.store', $project->id] 
    ]) 
!!} 

<div class="form-group"> 
    {!! Form::label('documentType', 'Document Type:', array('class' => 'col-sm-5 control-label blue')) !!} 
    <div class="col-sm-7"> 
     {!! Form::textArea('documentType', null, array('class' => 'form-control')) !!} 
    </div> 
</div> 
<div class="form-group"> 
    {!! Form::label('documentContent', 'Document Data:', array('class' => 'col-sm-5 control-label blue')) !!} 
    <div class="col-sm-7"> 
     {!! Form::textArea('documentContent', null, array('class' => 'form-control')) !!} 
    </div> 
</div> 
<div class="form-group"> 
    {!! Form::submit('Save Data', ['class' => 'btn btn-primary']) !!} 
</div> 

{!! Form::close() !!} 

所以调用的文件存储功能将数据保存到数据库中。

这一切工作正常,数据保存以及projectId,所以我能够链接到一个项目的文件。但是,如果我现在再次访问该项目的展示页面,则表单空白。如果我填写这个表单,它会将第二行保存到具有与第一行相同projectId的数据库中。

如果允许我为一个项目创建两个文档,这是否与一对一关系不矛盾?我如何确保只保存一个文档,并且下一次访问项目的显示页时,它可能会有一个按钮来显示文档。

感谢

+0

为什么不能ü保存专案编号会话,并用它在你制作的看法? –

+0

该文件可能不会立即生成,除非我在这里忽略了这一点? –

+0

这就显示了这一点。将projectId存储在会话中并将其用于创建文档。当你这样做时,将documentId存储在会话中,然后将其用于视图。 PLZ告诉我,如果你需要示例 –

回答

1

假设ü为了使项目做到这些:

$project = new Project(); 
$project->contactName = 'something'; 
$project->projectName = 'somethingElse'; 
$project->save(); //So you will have the id now! 

return redirect()->intended('showPage')->with('project_id' , $project->id); 

所以,你将被重定向到SHOWPAGE和专案编号存储在会话。现在在showPage中您有另一种形式来制作文档。但首先检索ü刚才这样创建的项目:

if(session()->has('project_id')){ 
    $pId = session('project_id'); 
    $project = Project::find($pId); 
} 

现在可以轻松地使用项目尽可能多的文件ü希望! 接下来我们假设你注册了文档并把documntId放入了会话中。

Session::put('documentId' , $document->id); 
在显示页面的顶部

所以只是增加一些检查:

$result = DB::table('document') 
      ->where(['id'=>Session::get('documentId') , 'projectId'=>$project->id])->get(); 
if($result != null){ 
// so just show the information not the submit form! 
} 
else{ 
//show the form to submit the document! 
} 
+0

感谢您的信息,帮助了很多。一个问题 - 据我所知,一个会话将持续,而应用程序是开放的。如果我为项目创建文档,然后关闭应用程序会发生什么?下一次我访问显示页面时,因为没有文档会话,它会再次显示创建表单? –

+1

很高兴帮助你!就像我在答案的最后部分所说的那样,为了确定是否有任何与项目相关的文档,您可以简单地从DB获取查询。另一种方法是修复这样的会话:一种解决方法是检查您的php.ini文件。你可以定义这个变量:session.gc_maxlifetime。默认情况下,它被设置为1440.只需发表评论或删除它。 从这个时候开始,你可以使用session.php配置值正常工作。 –