我正在尝试使用Laravel 4.2和Elequent ORM从表中删除数据,但它无法正常工作

时间:2016-05-02 13:41:53

标签: laravel eloquent sql-delete

我正在尝试使用Laravel 4.2Elequent ORM//delete incoming calls against this number \DB::table('incoming_call') ->where("contact_num",'=',$data['mobile']) ->delete(); //delete outgoing calls against this number \DB::table('outgoing_call') ->where("contact_num",'=',$data['mobile']) ->delete(); ` 删除数据。但是我无法删除,这是我的代码:

Public Sub TransferPivotCache(SourcePivot As PivotTable, TargetPivot As PivotTable)
  TargetPivot.CacheIndex = SourcePivot.CacheIndex
End Sub

1 个答案:

答案 0 :(得分:0)

这是正确的用法,但这只会删除一行:

\DB::table('incoming_call')
          ->where("contact_num",'=',$data['mobile'])
          ->first();
          ->delete();


\DB::table('outgoing_call')
          ->where("contact_num",'=',$data['mobile'])
          ->first();
          ->delete();

如果要删除多行,可以执行以下操作:

$coll = \DB::table('incoming_call')
              ->where("contact_num",'=',$data['mobile'])
              ->get();

\DB::table('incoming_call')->destroy($coll->keys());

或者只使用简单的foreach

$coll = \DB::table('incoming_call')
          ->where("contact_num",'=',$data['mobile'])
          ->get();

foreach($coll as $one){
    $one->delete();
}