我有2個表,客戶端和項目,並且項目與客戶端相關聯。客戶和項目都實施了軟刪除以維護關係,因爲存檔原因,即使我刪除了客戶,項目仍然會附加客戶信息。Laravel 4:雄辯軟刪除和關係
我的問題是,當我刪除客戶端時,引用變得無法從項目中訪問並引發異常。我想要做的是軟刪除客戶端,但保留項目關係中的客戶端數據。
我的刀片代碼如下:
@if ($projects->count())
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Client</th>
</tr>
</thead>
<tbody>
@foreach ($projects as $project)
<tr>
<td>{{{ $project->name }}}</td>
<td>{{{ $project->client->name }}}</td>
<td>{{ link_to_route('projects.edit', 'Edit', array($project->id), array('class' => 'btn btn-info')) }}</td>
<td>
{{ Form::open(array('method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
</td>
</tr>
@endforeach
</tbody>
</table> @else There are no projects @endif
下面是遷移:
Schema::create('clients', function(Blueprint $table) {
// Table engine
$table->engine = 'InnoDB';
// Increments
$table->increments('id');
// Relationships
// Fields
$table->string('name');
// Timestamps
$table->timestamps();
// Soft deletes
$table->softDeletes();
});
Schema::create('projects', function(Blueprint $table) {
// Table engine
$table->engine = 'InnoDB';
// Increments
$table->increments('id');
// Relationships
$table->integer ('client_id');
// Fields
$table->string('name');
// Timestamps
$table->timestamps();
// Soft deletes
$table->softDeletes();
// Indexes
$table->index('client_id');
});
非常感謝。
你試過使用':: withTrashed()'嗎? –
您可以顯示遷移嗎?如何以及你究竟想要刪除什麼? – carousel
我正在嘗試刪除(軟刪除)客戶端,但在查看與客戶端相關的項目時保留客戶端名稱。 – Wally