Laravel将数据数组插入数据库

时间:2018-09-25 15:21:33

标签: php database laravel laravel-5 eloquent

我的 Controller 中有这行代码,可以从Google Analytics(分析)获取数据

$analyticsData = Analytics::performQuery(Period::days(30),
    'ga:',
    [
        'metrics' => 'ga:users,ga:newUsers,ga:sessions,ga:sessionsPerUser,ga:pageviews,ga:pageviewsPerSession,ga:avgSessionDuration,ga:bounceRate',
        'start-date' => '2018-08-01',
        'end-date' => '2018-09-02',
        'dimensions' => 'ga:pageTitle',
    ]);

dd($analyticsData);就是我所得到的。

analytics

这是我的迁移查询。

    Schema::create('analytics', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('site_id');
        $table->string('site_url');
        $table->integer('report_id');
        $table->integer('analytics_view_id'); 
        $table->date('reportGenerated');
        $table->integer('gaUsers');
        $table->integer('gaNewUsers');
        $table->integer('gaSessions');
        $table->integer('gaSessionsPerUser');
        $table->integer('gaPageviews');
        $table->integer('gaPageviewsPerSession');
        $table->integer('gaAvgSession');
        $table->integer('gaBounceRate');

    });

现在,我的问题是如何将那些数组数据存储到数据库中?谢谢。

2 个答案:

答案 0 :(得分:1)

那会是这样的:

$model = Analytics::create([
   'gaUsers' => $analyticsData['ga:users'],
   'gaNewUsers' => $analyticsData['ga:newUsers'],
   // ... and so on...
]);

以防万一,请确保在$fillable模型的Analytics属性中具有这些字段。

答案 1 :(得分:1)

尝试:

  auditlog.event_date_time,
  auditlog.new_state,
  auditlog.previous_state,
  auditlog.id,
  auditlog.log_date_time,
  auditlog.refers_to,
  auditlog.user