【发布时间】:2018-02-19 02:35:33
【问题描述】:
试图在 DB 中存储对象数组并得到错误:array_merge(): Argument #1 is not an array
$tmp = new Projects;
$tmp->items = '{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}';
$tmp->save();
找到了很多答案,但没有一个有用。
更新(模型文件和迁移添加):
模型文件:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Projects extends Model
{
protected $casts = [
'items' => 'array',
];
}
迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProjectsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('gID', 100);
$table->string('name', 100);
$table->text('items');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('projects');
}
}
和功能:
$tmp = new Projects;
$tmp->name = "aaa";
$tmp->gID = "FLSKJDF3R23R";
$tmp->items = [["id" => "108124505876479", "name" => "Wakeboarding"],["id" => "aa", "name" => "volkswagen" ]];
$tmp->save();
【问题讨论】:
-
array_merge()在哪里使用? -
我看到缺少
[和]括号来包裹你的数组 -
(1/1) ErrorException array_merge(): Argument #1 is not an array in HasAttributes.php (line 769) at HandleExceptions->handleError(2, 'array_merge(): Argument #1 is不是数组', '/Applications/MAMP/htdocs/ezra-app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php', 769, array('defaults' => array(' created_at', 'updated_at')))