Reputation: 19463
I am trying to do database seed to my new database table.
ProductSeeder.php
class ProductSeeder extends DatabaseSeeder {
public function run()
{
$products = array(
array(
'name' => 'DSLR D300S (body only)',
'image' => 'D300S_16_85_front34l_i.jpg',
'stock' => 'Available',
'price'=>'5188.00'
)
);
foreach ($products as $product) {
Product::create($product);
}
}
}
model/Product.php
class Product extends Eloquent{
}
However, when I run db:seed
it says "Call to undefined method Product::create()".
How could I solve it? Thank you.
Upvotes: 3
Views: 4129
Reputation: 1814
Cannot say if its your problem, but i was getting this error due to a typo in my seeder file
Instead of
Product::create($product);
I had
// note only one semicolon
Product:create($product);
This it at any rate definately causes the same error !
PHP Fatal error: Call to undefined function create()
So i would definately recommend you check your code again
Also i was only extendting Seeder and not DatabaseSeeder So also try
class ProductSeeder extends Seeder {
Upvotes: 0
Reputation: 14620
When seeding your database relying on models isn't a particularly good idea, this comes to the forefront when you're trying to unit test (database test) your models through repositories.
Instead use the raw query builder (with the fluent interface).
DB::table('products')->insert(array(...));
Upvotes: 3
Reputation: 87789
The best practice to seed in Laravel is to use 'DB::table()':
class ProductSeeder extends DatabaseSeeder {
public function run()
{
$products = array(
array(
'name' => 'DSLR D300S (body only)',
'image' => 'D300S_16_85_front34l_i.jpg',
'stock' => 'Available',
'price'=>'5188.00'
)
);
foreach ($products as $product) {
DB::table('products')->insert($products);
}
}
}
Upvotes: 0
Reputation: 3658
Class Product needs to have a method Create.
class Product extends Eloquent{
public function create($product)
{
// Do something
}
}
Upvotes: -1