简体   繁体   English

如何在不使用Laravel中任何关系的情况下删除多表数据库中的数据

[英]How to delete data in multiple table database without using any relationship in Laravel

I have 2 table in my database. 我的数据库中有2个表。

first is table post 首先是表贴

Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->date('PostDate');
        $table->string('PostTitle', 200);
        $table->string('GroupID', 100)->nullable();
        $table->integer('LanguageID')->default(1);
        $table->timestamps();
    });

second is table post_categories 第二个是表post_categories

Schema::create('post_categories', function (Blueprint $table) {
        $table->integer('post_id');
        $table->integer('category_id');
        $table->timestamps();
    });

Every time I store post data into database, it will stored as two rows.. because I have two active language. 每次我将帖子数据存储到数据库中时,它都会存储为两行。.因为我有两种有效的语言。 In my post_categories table, there has category_id column.. So, if the post data with id = 1 has 2 categories.. and the post data with id = 2 has 3 categories, it will create 5 rows in post_categories table. 在我的post_categories表中,有category_id列。.因此,如果id = 1的post数据具有2个类别..而id = 2的post数据具有3个类别,则它将在post_categories表中创建5行。 But when I want to delete the post, I will check from the group ID first.. So, I will delete 2 rows too.. But the problem is, I can not delete the data in post_categories.. I have using foreach but the data won't be deleted.. 但是,当我要删除帖子时,我将首先从组ID中进行检查。因此,我也将删除2行。但是问题是,我无法删除post_categories中的数据。数据不会被删除。

This is my delete Controller: 这是我的删除控制器:

public function destroy($id)
{
    $post = Post::findOrFail($id);
    Post::where('GroupID', $post->GroupID)->delete();
    $postGroups = Post::where('GroupID', $post->GroupID)->get();
    foreach ($postGroups as $postGroup) {
        DB::table('post_categories')->where('post_id', $postGroup->id)->delete();
    }

    return response()->json([
        "error" => "0",
        "message" => "Success"
    ]);
}

I can delete all the post with same group ID, but I can not delete the post_categories data that has same post_id. 我可以删除具有相同组ID的所有帖子,但是不能删除具有相同post_id的post_categories数据。

You've already chosen the answer, but in your case it's much better to use whereIn() : 您已经选择了答案,但就您而言,最好使用whereIn()

$post = Post::findOrFail($id);
$postGroups = Post::where('GroupID', $post->GroupID)->get();
DB::table('post_categories')->whereIn('post_id', $postGroups->pluck('id'))->delete();
Post::where('GroupID', $post->GroupID)->delete();

Delete the categories before deleting the post. 在删除帖子之前,请删除类别。

public function destroy($id)
{
    $post = Post::findOrFail($id);

    $postGroups = Post::where('GroupID', $post->GroupID)->get();
    foreach ($postGroups as $postGroup) {
        DB::table('post_categories')->where('post_id', $postGroup->id)->delete();
    }

    Post::where('GroupID', $post->GroupID)->delete();

    return response()->json([
        "error" => "0",
        "message" => "Success"
    ]);

} }

In order to have a better code experience, I recommend using a table structure like posts , posts_categories and posts_languages . 为了获得更好的代码体验,我建议使用表结构,例如postsposts_categoriesposts_languages This last one will contain the data regarding the content in multiple languages, and will be related to posts 最后一个将以多种语言包含有关内容的数据,并将与posts相关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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