Laravel 4 (Eloquent) Error deleting records

Asked

Viewed 1,110 times

0

Hello, in this code below I delete the database of the selected records in a list of chekboxes.

I have an array of Ids that is mounted as follows:

Array (
    [0] => 810
    [1] => 811
)

On top of this array I do a foreach search each record and call the exclusion method, complete code below:

public function destroy_download($id = 0){
    $ids = ($id >= 1) ? array($id) : Input::get('ids');

    if( ! empty($ids) ){
        foreach ($ids as $id) {
            $download = Download::find($id);
            $download->delete();
        }
    }
}

This should work properly, however gives the following error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException Call to a member function delete() on a non-object

on the line:

$download->delete();

It’s like the record hasn’t been found.

It removes the record in the database only if this error happens, someone can imagine what it is?

The Array is correct, I’ve debugged but always gives it.

2 answers

1

Try the following:

public function destroy_download($id = 0){
    $ids = ($id >= 1) ? array($id) : Input::get('ids');

    if( ! empty($ids) ){
        foreach ($ids as $id) {
            try {
                Download::findOrFail($id)->delete();
            } catch (ModelNotFoundException $e){
                continue;
            }
        }
    }
}

This code will attempt to delete the id, if it does not find this record, it goes to the next.

Suggestion: I think it’s good to review this method, the way it is we have unnecessary variable checks $ids

public function destroyDownloads(){
    $ids = Input::get('ids');

    // Não é um array? Aborta a função
    if(!is_array($ids)){
        return false;
    }

    foreach ($ids as $id){
        $this->deleteDownload($id);
    }

    // Ou returne um Response::make()
    return true;

}

private function deleteDownload($id){

    try {
        Download::findOrFail($id)->delete();
    } catch (ModelNotFoundException $e){
        // Caso queira verificar erros no futuro, basta verificar com um if
        // o retorno de $this->deleteDownload($id), se for falso o registro não existia
        return false;
    }

    return true;
}
  • I tried to use the Try catch, but it does not release a Modelnotfoundexception exception. It gives error in $download->delete(), and does not enter the catch. This is bizarre. Here’s a printa for you to see: http://imageshack.com/img600/2163/dfzi.png

  • The strange thing is that if instead of calling the method $download->delete(), I do echo $download->name works, there is no error, it displays the names.

  • @Jarbas try to post a var_dump of the object $download and post your model Download

  • posted in Pastebin. Dump is the object without calling ->delete(), because there is no error. Object $download: http://pastebin.com/dwGcStJg Model: http://pastebin.com/vSDGsxt7

  • Strange... I’ll try to reproduce the error here

0

Confirm that the ID of the object exists, interpreting the result a little, looks like the function Download::find($id); returns a string/null when the Id does not exist, therefore when Voce tries to call the method delete() generates this error.

Create a check to confirm that the ID actually exists before calling delete().

  • Unfortunately that’s not it, I tested.

  • @Jarbas, I updated the answer

  • Rogerio, I have already done all the possible tests, tested if the object exists and etc. These ids come from a grid that lists the records of this table. The problem is that it deletes the record that gives error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.