Seeder Laravel with 2 relationships

1

I need a help, I want to create a Category for each Category and for each Local / strong>, but when running the seeds, the error below occurs:

  

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

Model Place

<?php

namespace Moviet\Models;

use Illuminate\Database\Eloquent\Model;

class Place extends Model
{
protected $fillable = [
    'city_id',
    'category_id',
    'name',
    'description'
];

public function category()
{
    return $this->belongsTo(Category::class);
}

public function cities()
{
    return $this->hasMany(City::class);
}
}

Model Category

<?php

namespace Moviet\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = [
      'name'
    ];

    public function places()
    {
        return $this->hasMany(Place::class);
    }
}

Model City

<?php

namespace Moviet\Models;

use Illuminate\Database\Eloquent\Model;

class City extends Model
{
    protected $fillable = [
        'name'
    ];

    public function place()
    {
        return $this->belongsTo(Place::class);
    }
}

Seeder

<?php

use Illuminate\Database\Seeder;
use Moviet\Models\Category;
use Moviet\Models\City;
use Moviet\Models\Place;

class CategoryTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(Category::class, 10)->create()->each(function($c) {
            for ($i = 0; $i <= 5; $i++){
                $c->places()->save(factory(Place::class)->create()->each(function($p){
                    $p->cities()->save(factory(City::class)->make());
                }));
            }
        });
    }
}

I'm using Laravel 5.1

    
asked by anonymous 10.06.2017 / 23:14

2 answers

1

To solve this problem, you have to keep in mind the logic to exist first, in case City and Category must exist for Local > may also exist, that is, a logical sequence.

% ideal%:

class CategoryTableSeeder extends Seeder
{   
    public function run()
    {
        factory(Category::class, 10)->create()->each(function($c) {

            factory(City::class, 1)->create()->each(function($a) use ($c)
            {
                factory(Place::class, 1)->create([
                    'name' => 'a',
                    'description' => 'a',
                    'city_id' => $a->id,
                    'category_id' => $c->id
                ]);
            });
        });
    }
}

It will create 10 records for all tables and relating the Seed of subsequent and sorted.

11.06.2017 / 00:13
0

To illustrate how to work with seeders we have the relationship between the company and bank tables%

Models     // Company Model     public function bank ()     {         return $ this-> hasMany (Bank :: class);     }

//Bank Model
public function company()
{
    return $this->belongsTo(Company::class);
}

ModelFactory

Note: Browse uses the 1-N file to create your factory in a separate file.

/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\Models\Company::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->company,
        'uuid' => $faker->uuid,
        'cnpj' => rand(10000000, 99999999) . '0001'
    ];
});

Note that in bank I did not need to pass the foreign key to faker "set" any value.

>
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\Models\Bank::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->company,
        'account' => $faker->randomNumber(6),
        'bank_agency' => $faker->randomNumber(4)
    ];
});

Seeder

//Seeder Company
public function run()
{
    factory(Company::class, 50)->create();
}

//Seeder banck
public function run()
{
    $company = Company::pluck('id');
    factory(Bank::class, 50)->make()
        ->each(function ($bank) use ($company) {
           $bank->company()->associate($company->random(1)->first());
           $bank->save();
        });
}
    
24.07.2017 / 21:34