简体   繁体   English

雄辩,删除不起作用,Laravel 5

[英]Eloquent, Delete not working, Laravel 5

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. 我正在使用eloquent和laravel 5开发Web应用程序。问题是我试图删除名为“ Ponderacion”的表的行,但是当我发送ajax删除请求时,服务器停止了(它停止了执行路由功能,但服务器保持运行)在删除所在的行,而不会引发任何错误。

Here is the Model of Ponderacion: 这是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. Ponderacion具有指向其他表的外键,但其他表均未引用Ponderacion。 I'm able to delete a row of Ponderacion with DB::statement but that is not secure.Succesfully deleted never shows on console. 我可以使用DB :: statement删除一行Ponderacion,但这并不安全。成功删除后在控制台上永远不会显示。

Thanks in advance. 提前致谢。

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. 对于AJAX测试,我总是更喜欢先通过GET请求直接进行测试,然后在我知道底层代码有效的情况下,再在AJAX POST上进行分层。

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. 在这种情况下,服务器可能会抛出Fatal Error ,然后针对AJAX请求将返回500错误,并且没有其他信息。 There is a good SO question that deals with catching Fatal Errors . 有一个很好的SO问题可以解决致命错误

I would check your includes for your class. 我会检查您的班级包括。 In Laravel 5, the default namespace is not the global namespace for Controllers. 在Laravel 5中,默认名称空间不是Controller的全局名称空间。 You'll need to add a \\ before Exception or add use Exception to the top of your class file. 您需要在Exception之前添加\\或在类文件的顶部添加use Exception

Two tips as well: 还有两个技巧:

  • Use SoftDeletes on your model. 在模型上使用SoftDeletes 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. 数据库存储空间确实很便宜,Laravel在查询时会自动跳过已删除的行。
  • Use an IDE for development. 使用IDE进行开发。 A lot of these errors can be caught before run-time by a good compiler. 一个好的编译器可以在运行前捕获很多此类错误。

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

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