Seeder Laravel with 2 relationships

Asked

Viewed 676 times

1

I need a help, I want to create, for each Category, one Local and for each Local, one City, but when running the Eds, the error occurs below:

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 wearing Laravel 5.1

2 answers

1

To solve this problem, you have to keep in mind the logic of existing first, in case, City and Category must exist in order to Local may also exist, i.e., a logical sequence.

Seed 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 id subsequent and ordered.

References

  • Thanks for the help, but the problem remains :(

  • Report the problem ???? @Eduardollythe code has been tested on top of your question! Tell me what’s showing up on the console!

  • The same error quoted at the beginning of the post, using its code, the problem occurs in the same way.

  • @Eduardopaludo the problem is difficult to reproduce as I warned you this code was tested and here it worked perfectly, as is in your question, makes the following command on the console php artisan cache:clear and try Seed again, and if it doesn’t work point out the error line, please paste the error into your question

0

To illustrate how to work with seeders we have the relationship between the tables company and bank 1-N:

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

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

Modelfactory

Note: Search uses the file ModelFactory.php to create their 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 the Factory of bank I didn’t need to pass the foreign key to the Faker "setar" 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();
        });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.