2017-12-27 14 views
0

したがって、私は2つのテーブル 'トピック'と '投稿'を持っています。Laravelコントローラの異なるラベルから2行を削除する

トピックはスレッドの主な内容とその回答の投稿用です。

ユーザーがトピックを削除した場合は、次の返信/投稿も削除する必要があります。ここで

は私の削除フォームです:

{!! Form::open(['action' => ['[email protected]', $topic->id], 'method' => 'POST', 'class' => 'pull-right']) !!} 
    {{ Form::hidden('_method', 'DELETE') }} 
    {{ Form::submit('Delete', ['class' => 'btn btn-danger']) }} 
{!! Form::close() !!} 

、ここでコントローラです:

$topic = Topic::find($id); 
$post = Post::where('topic_id', $id)->get(); 
$topic->delete(); 
$post->delete(); 
return redirect('/')->with('Success', 'Post Removed'); 

しかし、それはエラーを与えている:

BadMethodCallException 
Method delete does not exist. 

がここで間違っていたとは何ですか?

+0

お望みのものに応じてforceDelete()またはsoftDelete()を試してください – Cr1xus

答えて

1

カスケード削除を使用します。 the docsから

You may also specify the desired action for the "on delete" and "on update" properties of the constraint

postsテーブルの移行に外部キー制約を定義します。

$table->foreign('topic_id') 
     ->references('id') 
     ->on('topics') 
     ->onDelete('cascade'); 

は、テーブルを再作成し、トピックに関連するすべての記事は、自動的にトピックの削除時に削除されます。

https://laravel.com/docs/5.5/migrations#foreign-key-constraints

0

Docs

Topic::destroy($id); 
Post::where('topic_id', $id)->delete(); 
return redirect('/')->with('Success', 'Post Removed'); 
0

ではより多くの情報あなたは、トピック内のリレーションを使用して投稿し、各応答がポストに所属例えば表示するために、モデルを返信する必要があります。

関連する問題