dongou1970 2017-01-26 15:06
浏览 100
已采纳

删除Laravel中数据透视表中的行

I have two tables. Table reports

report_id | user_id | item_id

and reports_messages

report_id | user_id | item_id | messages

I want when I delete report_id on reports all related rows which matching report_id in reports_messages to be deleted too.

In my ReportMessages Model I have this relation

public function reports(){
    return $this->belongsTo('App\Report');
}

public function item(){
    return $this->belongsTo('App\Item', 'item_id', 'id');
}   

In Report model

public function reportedItem(){

    return $this->belongsTo('App\Item');
}

public function user(){
    return $this->hasOne('App\User', 'id', 'user_id');
}

So far I've tried this solution founded here on SO

public function destroy($report_id){

    Report::destroy($report_id);
    ReportMessages::find(1)->reports()->where('report_id',$report_id)->delete();

    return redirect()->route('user.ports');

This deletes only in reports.. doesn't delete related report_id's in pivot table. }

  • 写回答

2条回答 默认 最新

  • duansaxf095988 2017-01-26 15:08
    关注

    Laravel has the functions detach and attach to deal with pivot tables. So you can do this to remove the record in the pivot table:

    ReportMessages::find(1)->reports()->detach($report_id);
    

    This will however not remove the row in the reports table because it could still be linked to another object.

    Update:
    So, I just noticed, you don't have pivot tables, you only have two models that are linked.

    You don't have to load the reports() relation in your query to remove the ReportMessages, you can just do it like this:

    Report::destroy($report_id);
    ReportMessages::where('report_id',$report_id)->delete();
    

    This will remove the report, and all corresponding reportmessages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douxiuyu2028 2018-07-08 13:23
    关注

    I'm know I'm late but the best option with this kind of relationship, it is best to define foreign keys to particular linking tables in your table " reports_messages" like below

    > $table->integer('report_id')->unsigned()->index(); $table->foreign('report_id')->references('id')->on('reports')->onDelete('cascade'); $table->integer('message_id')->unsigned()->index(); $table->foreign('message_id')->references('id')->on('messages')->onDelete('cascade');

    When you do something like this, the pivot table data will be removed as long as either message or report is deleted from database without a need of detaching methods. Best option in case message or report has relationship with other data,such that when data is deleted message should be deleted automatically, in this case we do not have to worry about detaching data again

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于PROCEDURE和FUNCTION的问题
  • ¥100 webapi的部署(标签-服务器)
  • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
  • ¥15 C语言除0问题的检测方法
  • ¥15 为什么四分管的内径有的是16mm有的15mm,四分不应该是12.7mm吗
  • ¥15 macos13下 ios交叉编译的问题
  • ¥15 bgz压缩文件怎么打开
  • ¥15 封装dll(引入了pcl的点云设计库)
  • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发
  • ¥15 oracle 多个括号,怎么删除指定的括号及里面的内容?