2017-10-01 1 views
0

モデルテーブルからモデルを削除しようとしています。コメントテーブルからそのすべてのコメントを削除しようとしています...現在のコードはモデルを削除します。ここで。Load()を使用してすべてのテーブルからデータを削除します。

は私の機能

public function destroy(Request $request,CadModel $cadmodel) 
    { 
     $cadmodel->load('comments')->delete(); 
     $request->session()->flash('message.level', 'success'); 
     $request->session()->flash('message.content', 'File Deleted Successfully!'); 
     return redirect()->route('dashboard'); 
    } 

ここで私はCadModel Laravelモデルで定義された関係があるの破壊である...

public function comments() 
    { 
     return $this->hasMany(Comment::class); 
    } 

both..Modを削除するには、返信、私に解決策を提案してください。 elとそのコメント

+0

あなたが雄弁を使用することができます。これを確認してくださいhttps://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm – kawadhiya21

+0

私はEloquentを使用しないで解決策が必要です.. – ma123456

+0

あなたは自分の解決策を書く必要がありますかと思います – kawadhiya21

答えて

0

onDelete('cascade')でモデルが削除された場合、関連するコメントを削除するように設定します。

例:

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('comments_model', function(Blueprint $table) 
     { 
      $table->increments('id'); 
      $table->integer('comment_id')->unsigned(); 
      $table->integer('model_id')->unsigned(); 

      $table->foreign('comment_id')->references('id')->on('comments')->onDelete('cascade'); 
      $table->foreign('model_id')->references('id')->on('models')->onDelete('cascade'); 
     }); 
    } 
関連する問題