Laravel 未定义索引:

Laravel Undefined index:

我正在使用我在 laravel 中的函数有问题,似乎是数组问题,但两天后我无法弄清楚错误消息是什么

Undefined index: eventID

我的函数

public static function DeleteRow($id){
    $data=DB::select("select * from InOutProducts where eventID=$id;"); 
    $eventID=$data['eventID'];
    $productID=$data['productID'];
    $username=Session::get('key');  
    DB::delete("delete from InOutProducts where eventID=$eventID");
    $row=DB::select("select sum(quantity) as total_quantity from  InOutProducts  where productID='$productID' and InAndOut='1'");
    $PhysicalStock=$row[0]->total_quantity;
    DB::update("update productsStock set physicalStock=$PhysicalStock,lastUpdateBy='$username' where productID=$productID;");`

}

DB::select returns 一个数组。试试这个:

$data = DB::select("select * from InOutProducts where eventID=$id;"); 
$data = $data[0];
$eventID = $data->eventID;

如果你 var_dump $data,它很可能是空的,

您必须从查询字符串中删除 semi colin。

$data=DB::select("select * from InOutProducts where eventID=$id;"); 

至此

$data=DB::select("select * from InOutProducts where eventID=$id"); 

建议,你的 DeleteRow 函数正在做

  1. select
  2. 删除
  3. select
  4. 更新

你不觉得在一个函数中做的事情太多了吗? 也许你可以看看 Laravel 如何支持查询数据库? Laravel query the database