简体   繁体   中英

How to delete duplicated row in Laravel 5.2

I try to delete duplicated row in my code with :

$post = new posts();
$post->raw('DELETE b1 FROM baroot b1,baroot b2 WHERE b1.post_id = b2.post_id AND b1.id > b2.id');

and my model code is :

class posts extends Model
{
    protected $table = 'baroot';

    protected $fillable = [
        'post_id', 'title', 'modify',
    ];
}

I try to delete duplicated with :

$post = new posts();
$post->raw('DELETE b1 FROM posts b1,posts b2 WHERE b1.post_id = b2.post_id AND b1.id > b2.id');

but any duplicated rows not deleted, I know the sql query is correct but don't work with my laravel code

From the documentation of " Running Raw SQL Queries "

Running A Delete Statement

The delete method should be used to delete records from the database. Like update, the number of rows deleted will be returned:

$deleted = DB::delete('delete from users');

So you have to execute

$deleted = DB::delete('DELETE b1 FROM baroot b1,baroot b2 WHERE b1.post_id = b2.post_id AND b1.id > b2.id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM