当lason上的Json数据种子时,试图获取非对象的属性

问题描述 投票:0回答:1

我想在laravel中使用播种机播种json数据。当我使用php artisan migrate --seed命令迁移和播种时,下面的消息将出现在命令行中

In PaymentTableSeeder.php line 19:

  Trying to get property 'identity' of non-object 

付款gateway.json

 [  
   {  
      "identity":"Stripe",
      "config":{  
         "Publishable key":"",
         "Secret key":""
      },
      "status":0
   }
]

PaymentTableSeeder

<?php

use Illuminate\Database\Seeder;

class PaymentTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $json = File::get('database/data/payment-gateway.json');
        $data = json_decode($json, true);

        foreach($data as $obj){
            DB::table('payment_settings')->insert([
                'identity' => $obj->identity,
                'config' => $obj->config,
                'status' => $obj->status
            ]);
        }

    }
}
php json laravel
1个回答
5
投票
 $data = json_decode($json, true);

第二个参数表示您将结果作为assoc数组获得。删除参数或通过访问数据

$obj['identity']

请参阅文档here

© www.soinside.com 2019 - 2024. All rights reserved.