我想了解一些东西。我有一个有特定领域的项目表。然后我有一个文档表,它有一个项目的外键。为了证明,我有以下模式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的数据库中。
如果允许我为一个项目创建两个文档,这是否与一对一关系不矛盾?我如何确保只保存一个文档,并且下一次访问项目的显示页时,它可能会有一个按钮来显示文档。
感谢
为什么不能ü保存专案编号会话,并用它在你制作的看法? –
该文件可能不会立即生成,除非我在这里忽略了这一点? –
这就显示了这一点。将projectId存储在会话中并将其用于创建文档。当你这样做时,将documentId存储在会话中,然后将其用于视图。 PLZ告诉我,如果你需要示例 –