【问题标题】:Laravel Undefined index:Laravel 未定义索引:
【发布时间】:2015-03-23 09:33:03
【问题描述】:

我正在使用我在 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;");`

}

【问题讨论】:

    标签: php mysql arrays laravel laravel-4


    【解决方案1】:

    DB::select 返回一个数组。试试这个:

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

    【讨论】:

    • 错误信息现在Cannot use object of type stdClass as array
    • 你必须在之后将其用作对象。 $data->eventID; $data->productID; 答案已更新。
    • 不客气。我建议你看看query builder 和/或eloquent models
    【解决方案2】:

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

    您必须从查询字符串中删除分号。

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

    到这里

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

    建议,你的 DeleteRow 函数正在做

    1. 选择
    2. 删除
    3. 选择
    4. 更新

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

    【讨论】:

    • 我 100% 确定 $data 不为空,因为我已经测试这是 $data 变量 [{"eventID":"160","productID":"45","product_reference":"001","productName":"HP OFFICEJET MAGINTA 5800","brand":"HP","model":"C11234","quantity":"11","location":"Container 1","actiontime":"2015-01-22 08:10:55","username":"osacapcom","InAndOut":"1"}] 关于单个函数的内容,也许我会在下一个 Web 应用程序中这样做.
    • 你得到的响应是一个json,你应该做json_decode($data)把json转换成一个数组。
    猜你喜欢
    • 2018-08-02
    • 2012-08-24
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2021-10-06
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多