0

Trying to store array of objects in DB and getting an error : 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();

Found a lot of answers, but no one is useful.

Update (model file and migration added):

Model file:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Projects extends Model
{
    protected $casts = [
        'items' => 'array',
    ];

}

Migration file:

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

and the function:

$tmp = new Projects;
$tmp->name = "aaa";
$tmp->gID = "FLSKJDF3R23R";
$tmp->items = [["id" => "108124505876479", "name" => "Wakeboarding"],["id" =>  "aa", "name" => "volkswagen" ]];
$tmp->save();
3
  • where is array_merge() used ? Commented Sep 10, 2017 at 21:52
  • I see missing [ and ] brackets to wrap your array Commented Sep 10, 2017 at 21:55
  • (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 not an array', '/Applications/MAMP/htdocs/ezra-app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php', 769, array('defaults' => array('created_at', 'updated_at'))) Commented Sep 10, 2017 at 22:23

1 Answer 1

1

I'm assuming your Projects class is an Eloquent Model. With the code given, it is not clear how items is mapped to MySQL and whether you told your model that you want to use items as an array.

In your migration, you can set it to be a text or json field. E.g.

$table->text('items');

Then in your model, you can declare it as an array:

protected $casts = [
    'items' => 'array',
];

Now Laravel's Eloquent Model takes care of mapping your array to a string when it gets stored to the DB and vice versa.

As you get an array_merge error, I assume you actually set it up this way and Eloquent causes this error, as you pass in a string.

So you probably can just call:

$tmp->items = [["id" => "108124505876479", "name" => "Wakeboarding"],["id" => ... ... ]];

See Eloquent's casting capabilities: Array and Json casting

Sign up to request clarification or add additional context in comments.

7 Comments

copied your example and: "Array to string conversion"
@aleXela: what do you mean with "Array to string conversion"?
It would help if you could add your Migration and Model code to your question.
added, both model and migration
You may want to set the table name protected $table = 'projects', as your class name is plural. From the documentation: By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. .Not sure how this works when the class name is already plural.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.