2

In my Laravel Controller, I'm importing csv to my db, I query SQL get products_name and then insert product_name to my order table. The CODE work Perfect on my xampp server, it was when I deploy to live server (Cpanel) that I gives Error.

$products_name = DB::table('products_description')
->where('products_id','=', $importData[0])->pluck('products_name');


OrdersProduct::create(
                       array
                        (
                            "products_id"=>$importData[0],
                            "products_name"=>$products_name[0],
                            "products_quantity"=>$importData[1],
                        )
                    );

My Larevel Log is showing Error Msg: production.ERROR: Undefined offset: 0

When I used return $products_name[0], It displays the Product Name... but when I tried to Create OrderProducts, it generates error. Note there is content is the products_name and it works fine on my localhost, only when I deployed to server I get error.

Lamp Soft
  • 53
  • 6
  • https://stackoverflow.com/questions/37404256/laravel-undefined-offset-0-collection-php – my_workbench Aug 04 '20 at 07:25
  • Does this answer your question? [Laravel - Undefined offset 0 - collection.php](https://stackoverflow.com/questions/37404256/laravel-undefined-offset-0-collection-php) – N69S Aug 04 '20 at 07:33
  • You're certain that it should work on the live server because it worked and data is present on your local setup ? those two are unrelated. Have you checked the data on the live server too ? did you check the error line ? – N69S Aug 04 '20 at 07:34
  • Try `$products_name = DB::table('products_description') ->where('products_id','=', $importData[0])->first();` then use `$products_name->products_name` instead of `$products_name[0]` – STA Aug 04 '20 at 07:44
  • Where does `$importData` come from? – apokryfos Aug 04 '20 at 07:47
  • you probably get your array with filter or something like it, you can use: $myarray = array_values($myarray); to reindex your array, see https://stackoverflow.com/a/7558038/10573560 – OMR Aug 04 '20 at 07:49
  • This exception is thrown when there is no value stored in a given index. This should be telling you that your SQL query returned `null` or `false`. You should check that the product name exists using `isset()`. If it should exist, then ensure your "live" server has the corresponding data to your localhost – Jaquarh Aug 04 '20 at 08:47
  • @apokryfos $importData is from another query and it is working. This same issue happen even if I change the $importData to a static number. – Lamp Soft Aug 04 '20 at 14:25
  • @Jaquarh The Product name Exists, if I used to return, it will display the value, but when I tried to insert it to db is when it throw the error. The product name is Not null. – Lamp Soft Aug 04 '20 at 14:29
  • If you change `pluck('products_name')` to `value('products_name')` and then use `$products_name` (not an array) would that work? – apokryfos Aug 04 '20 at 14:47

0 Answers0