LARAVEL:销毁和分离方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41359938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:01:12  来源:igfitidea点击:

LARAVEL : Destroy and detach method

phplaraveleloquent

提问by Patrick Rey

I'm trying to delete a Poll with this function where I need to detach relations :

我正在尝试使用此功能删除一个 Poll,我需要在其中分离关系:

public function destroy($id)
{
    $poll = Poll::findOrFail($id);
    $poll->answers()->detach();
    $poll->poll_user()->detach();
    $poll->delete();
}

But I'm getting this error message, I don't know why :

但是我收到此错误消息,我不知道为什么:

Call to undefined method Illuminate\Database\Query\Builder::detach()

调用未定义的方法 Illuminate\Database\Query\Builder::detach()

Here is the Poll model :

这是民意调查模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Poll extends Model
{

    /**
     * La table utilisée par le modèle
     *
     * @var string
     */
    protected $table = 'polls';

    /**
     * On utilise les dates de mise à jour
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * Attributs autorisés en ensignement de masse.
     *
     * @var array
     */
    protected $fillable = ['question'];

    /**
     * Relation de type 1:n
     *
     * @return Illuminate\Database\Eloquent\Relations
     */
    public function answers()
    {
        return $this->hasMany('App\Models\Answer');
    }

    /**
     * Relation de type n:n
     *
     * @return Illuminate\Database\Eloquent\Relations
     */
    public function users()
    {
        return $this->belongsToMany('App\Models\User')->withTimestamps();
    }
 }

回答by CUGreen

Try the following code:

试试下面的代码:

public function destroy($id)
{
    $poll = Poll::findOrFail($id);
    $poll->answers()->dissociate();
    $poll->users()->sync([]);
    $poll->delete();
}

If that doesn't work then might need to look at your other models or migrations.

如果这不起作用,那么可能需要查看您的其他模型或迁移。

回答by Patrick Rey

Well I don't know if it's correct or not but I've done the following and it works perfectly :

好吧,我不知道它是否正确,但我已经完成了以下操作并且效果很好:

public function destroy($id)
{
    $poll = Poll::findOrFail($id);
    $poll->answers()->delete();

    $poll->users()->detach();
    $poll->delete();
}

回答by 4lban

I suppose you set up connections in the right way in your models and I think you are trying to call this method in parent Controller (not in child Controller). Based on these this code should work...

我想您在模型中以正确的方式设置了连接,并且我认为您正试图在父控制器(而不是子控制器)中调用此方法。基于这些,这段代码应该可以工作......

public function destroy($id)
{
    $poll = Poll::findOrFail($id);

    foreach ($poll->answers as $answer) 
    {
        $answer->poll()->dissociate();
        $answer->save();
    }

    foreach ($poll->user as $user) 
    {
        $user->poll()->dissociate();
        $user->save();
    }

    $poll->delete();
}

回答by Houssine Sghaire

/**
 * Removing an entity
 *
 * @param  integer $id
 * @return void
 */
public function destroy($id)
{
    $this->getById($id)->users()->detach();
    $this->getById($id)->answers()->delete();
    $this->getById($id)->delete();
}

/**
 * Getting an entity by id
 *
 * @param  integer $id
 * @return void
 */
public function getById($id)
{
    return Poll::find($id);
}