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
Thanks for the help, but the problem remains :(
– Eduardo Paludo
Report the problem ???? @Eduardollythe code has been tested on top of your question! Tell me what’s showing up on the console!
– novic
The same error quoted at the beginning of the post, using its code, the problem occurs in the same way.
– Eduardo Paludo
@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– novic