简体   繁体   English

如何在laravel eloquent中基于一对多关系中的单个列删除所有行?

[英]How do i delete all rows based on a single column in one to many relationship in laravel eloquent?

When deleting a particular movie , i want to delete all showtime related to it.删除特定电影时,我想删除与其相关的所有放映时间。 I have 3 tables related to this query.我有 3 个与此查询相关的表。

  1. Showtime (which has mm relation with times table (pivot table showtime_time) Showtime(与时间表有 mm 关系(数据透视表 showtime_time)
  2. Times时代
  3. Movies电影

What i tried is:我试过的是:

MovieController:电影控制器:

public function destroy($id)
{
    $movie = Movie::findOrFail($id);
    if (ShowTime::where('movie_id', '=', $id)->exists()) {
        $showtimes = ShowTime::whereIn('movie_id', '=', $id)->get();
        foreach ($showtimes as $showtime) {
            $showtime->times()->detach();
            $showtime->delete();
            $movie->delete();
        }
        return back();
    } else {
        $movie->delete();
    }
    return redirect()->route('movies.index');

}

Which gave me this error.这给了我这个错误。

Argument 1 passed to Illuminate\\Database\\Query\\Builder::cleanBindings() must be of the type array, string given, called in C:\\Users\\lenovo\\cinetime_nepal_backend\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query\\Builder.php on line 907传递给 Illuminate\\Database\\Query\\Builder::cleanBindings() 的参数 1 必须是数组类型,给定字符串,在 C:\\Users\\lenovo\\cinetime_nepal_backend\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query 中调用\\Builder.php 第 907 行

Model class模型类

<?php

namespace Modules\Movie\Entities;
use Illuminate\Database\Eloquent\Model;
class Movie extends Model
{
    protected $guarded = ['id']; //fillable
    protected $hidden = ['created_at', 'updated_at'];

    public function showTimes()
    {
        return $this->hasMany(ShowTime::class, 'movie_id');
    }
}

This is how i am deleting showtime in showtimecontroller这就是我在 showtimecontroller 中删除 showtime 的方式

ShowtimeController放映时间控制器

public function destroy($id)
{
    $showtime = ShowTime::findOrFail($id);
    $showtime->times()->detach();
    $showtime->delete();
    return back();
}

I am new to laravel, i searched and tried many codes answered in this site but could not help myself.我是 laravel 的新手,我搜索并尝试了本网站中回答的许多代码,但无法帮助自己。 Help Please请帮忙

If you're using MySQL you can add 'ON DELETE CASCADE' to your foreign key.如果您使用 MySQL,您可以将“ON DELETE CASCADE”添加到您的外键。 This will automatically delete any records that reference the parent when the parent is deleted.这将在删除父项时自动删除引用父项的所有记录。 just put it to your showtime migration只需将其放入您的放映时间迁移中

$table->foreign('movie_id')->references('id')->on('movies')->onDelete('cascade');

Or, if you don't want that approach, then you can overwrite your delete method in your movie model.或者,如果您不想要这种方法,那么您可以在您的电影模型中覆盖您的删除方法。 Put this code in your movie model.将此代码放入您的电影模型中。

public function delete() {
    $this->showTimes()->times()->delete();
    $this->showTimes()->delete();
    parent::delete();
}

Then in your controller you just delete your movie and it's all fine.然后在您的控制器中,您只需删除您的电影就可以了。

public function destroy($id)
{
    $movie = Movie::findOrFail($id);
    $movie->delete();

    return redirect()->route('movies.index');

}

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

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