1

i'm working on a web app using eloquent and laravel 5. The problem is that i'm trying to delete a row of a table called "Ponderacion" but when i send the ajax delete request, the server stops (it stops the execution of the routed function but the server keeps running) at the line where the delete is, without throwing any errors.

Here is the Model of Ponderacion:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Ponderacion extends Model{
    protected $table = 'Ponderacion';
    protected $fillable = array('ponderacionidearea', 'ConfiguracionDeExamenifconf', 'peso');
    public $timestamps = false;
}

Here is the function in the Controller:

public function deleteConfig(Request $request){
            error_log('deleting');
            $s_area = Area::where('nombre', '=', $request->input('s_area'))->first();
            error_log(count($s_area->Configuracion->first()->Ponderacion));
            //DB::statement('delete from Ponderacion where idponderacion = 22');
            foreach ($s_area->Configuracion->first()->Ponderacion as $ponderacion){

                error_log($ponderacion->peso);
                try{
                    $ponderacion->delete();
                }catch(Exception $e){
                    error_log('failure');
                }
            }
            //$s_area->Configuracion->first()->Ponderacion->delete();
            error_log('succesfully deleted');
            $s_area->Configuracion->first()->delete();

        }

I can succesfully print the property "peso" of ponderacion but i'm unable to delete it. Ponderacion has Foreign Keys to other table but no other table has a reference to Ponderacion. I'm able to delete a row of Ponderacion with DB::statement but that is not secure.Succesfully deleted never shows on console.

Thanks in advance.

Pedro Aim
  • 11
  • 2

1 Answers1

0

For AJAX testing, I always prefer to directly test via a GET request first where possible and then layer on the AJAX POST once I know the underlying code works.

In this case, the server is likely throwing a Fatal Error, which will then return a 500 error for an AJAX request and no further information. There is a good SO question that deals with catching Fatal Errors.

I would check your includes for your class. In Laravel 5, the default namespace is not the global namespace for Controllers. You'll need to add a \ before Exception or add use Exception to the top of your class file.

Two tips as well:

  • Use SoftDeletes on your model. It's better to track and audit your database records if you never really remove a row. DB storage space is really cheap and Laravel will automatically skip the deleted rows when querying.
  • Use an IDE for development. A lot of these errors can be caught before run-time by a good compiler.
Community
  • 1
  • 1
Adam Link
  • 2,783
  • 4
  • 30
  • 44