7

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

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()

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();
    }
 }
11
  • 1
    What is your poll/answers relationship? Can you post your Poll model? Commented Dec 28, 2016 at 10:29
  • oneToMany, 1-poll, n-answers Commented Dec 28, 2016 at 10:31
  • What version of Laravel? You sure you don't need to use dissociate() instead of detach()? Commented Dec 28, 2016 at 10:34
  • detach() is for many to many relation. Commented Dec 28, 2016 at 10:36
  • I've tried also with dissociate, i get the same error : Call to undefined method Illuminate\Database\Query\Builder::dissociate(), version is 5.2.6 Commented Dec 28, 2016 at 10:39

4 Answers 4

3
/**
 * 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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Could you translate these comments to english?
1

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();
}

3 Comments

maybe it's because an answer cannot have a poll set with null. but the error message is not explicit...
As long as it works. Others might have more insight.
This works because the Poll has a oneToMany relationship with the answers. Detach only works on manyToMany relationships like users()
0

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.

4 Comments

I've got the same xD : Call to undefined method Illuminate\Database\Query\Builder::sync()
well, it works without detaching the relations, but I think that's not correct at all
sorry, had sync on 1-m by mistake
So it works? Probably because you have cascade on delete? Depends on your migrations.
0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.