繁体   English   中英

从json文件播种laravel应用程序时插入空值

[英]Inserting null values when seeding laravel app from json file

在我的 Laravel 应用程序中,我有一个 json 文件,我想用它来将一些产品播种到我的数据库中。 出于某种原因,当我尝试保存产品时,我为 title 和 productcategory_id 字段插入了空值,即使认为数组值在那里,json 也会被转换为带有 json_decode 的数组。

这是我的逻辑:

<?php
use Illuminate\Database\Seeder;
use App\Models\Product;


class ProductsSeeder extends Seeder
{


    public function run()
    {
        $json = File::get(base_path().'/json/Products.json');

        $products = json_decode($json, true);

        var_dump($products);

        foreach ($products as $product)
        {
            echo($product['title']);
            echo($product['productcategory_id']);
            $product = new Product();
            $product->title = $product['title'];
            $product->productcategory_id = $product['productcategory_id'];
            $product->save();
        }
    }
}

我的 json 文件的摘录:

[
    { "productcategory_id" : 1, "title":"Hamb.Ternera Maxi"},
    { "productcategory_id" : 1, "title":"Hamb. Ternera Doble"}, 
    { "productcategory_id" : 1, "title":"Hamb. Pollo"}, 
    { "productcategory_id" : 1, "title":"Hamb. Mini"}, 
    { "productcategory_id" : 1, "title":"Camp. clásico"}, 
    { "productcategory_id" : 1, "title":"Camp. ternera"}, 
]

如您所见,我使用 var_dump 来查看 json 数据是否正确转换为 php assoc 数组,但我仍然收到错误消息。 我的 shell 中的完整错误消息是:

   Illuminate\Database\QueryException  : SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: insert into `products` (`title`, `productcategory_id`, `updated_at`, `created_at`) values (, , 2020-02-16 20:25:51, 2020-02-16 20:25:51))

  at C:\xampp\htdocs\dtcburger\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null")
      C:\xampp\htdocs\dtcburger\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\dtcburger\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  Please use the argument -v to see more details.

问题可能是您使用相同的变量来循环并获取值。 试试这样:

foreach ($products as $product)
    {
        echo($product['title']);
        echo($product['productcategory_id']);
        $product_n = new Product();
        $product_n->title = $product['title'];
        $product_n->productcategory_id = $product['productcategory_id'];
        $product_n->save();
    }

暂无
暂无

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

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