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