lundi 29 janvier 2018

Call to undefined method Illuminate\Database\Query\Builder::save() on seed

I'm trying to create seeders to my models using seed and factory. My models have relationship one to many. When I run

php artisan db:seed --class=ChildTableSeeder

I get the following error:

In Builder.php line 2461:

  Call to undefined method Illuminate\Database\Query\Builder::save()

Independently of the error the data seed is added to the target table.

My table's structure is like follow:

parents
        - parentId
        - name
        - value

    childs
        - childId
        - parentId
        - views
        - count

And my models:

//Parent.php
<?php

namespace App\Models\Api;

use Illuminate\Database\Eloquent\Model;

class Parent extends Model
{
    protected $primaryKey = 'parentId';
    public $incrementing = false;
    protected $keyType = 'string';
}


//Child.php
<?php

namespace App\Models\Api;

use Illuminate\Database\Eloquent\Model;

class Child extends Model
{
    protected $primaryKey = 'childId';
    public $incrementing = false;
    protected $keyType = 'string';

    public function parents()
    {
        return $this->belongsTo('App\Models\Api\Parent', 'parentId', 'parentId');
    }
}

Also see the Seed code

//ParentTableSeeder
factory(App\Models\Api\Child::class, 50)->create()->each(function ($u) {
        $u->parents()->save(factory(App\Models\Api\Parent::class)->make());
});

Anyone has an idea what I'm doing wrong? or How to solve this issue?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2GtKxTo
via IFTTT

Aucun commentaire:

Enregistrer un commentaire