Home » Php » php – Inserting null values when seeding laravel app from json file

php – Inserting null values when seeding laravel app from json file

Posted by: admin February 25, 2020 Leave a comment

Questions:

In my laravel app I have a json file I’m tying to use to seed some products into my database. For some reason when I try to save a product I’m inserting empty values for title and productcategory_id fields, even thought the array values are there, the json is transformed into an array with json_decode all right.

This is my logic:

<?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();
        }
    }
}

An excerpt of my json file:

[
    { "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"}, 
]

As you can see I use var_dump to see the json data is converted correctly into php assoc array, bt I still get the error. Full error message in my shell is:

   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.
How to&Answers:

The problem is probably that you are using the same variable to loop and to get the value.
try like this:

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();
    }