PHP Fatal error in laravel DatabaseSeeder

987

Hi Guys im newly learning laravel so when i setup code to seed my tables in database, i get this error

Seeding: BearAppSeeder
PHP Fatal error:  Call to undefined method Bear::create() in C:\Users\mrosh\Desktop\WD\garden\database\seeds\Database
Seeder.php on line 41


  [Symfony\Component\Debug\Exception\FatalErrorException]
  Call to undefined method Bear::create()

Here is my code in DatabaseSeeder.php file, the idea is to create table for 'bears', 'fish', 'trees' and 'picnics' and links bears to picnics "many-to-many" and seed them with some data

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Eloquent::unguard();

        // call our class and run our seeds
        $this->call('BearAppSeeder');
        $this->command->info('Bear app seeds finished.'); // show information in the command line after everything is run
    }

}

// our own seeder class
// usually this would be its own file
class BearAppSeeder extends Seeder {

    public function run() {

        // clear our database

so how i can fix this i have tried many things but all fails

350

Answer

-- DB::table('bears')->delete(); DB::table('fish')->delete(); DB::table('picnics')->delete(); DB::table('trees')->delete(); DB::table('bears_picnics')->delete(); // seed our bears table
169

Answer

--- // we'll create three different bears // bear 1 is named Lawly. She is extremely dangerous. Especially when hungry. $bearLawly = Bear::create(array( 'name' => 'Lawly', 'type' => 'Grizzly', 'danger_level' => 8 )); // bear 2 is named Cerms. He has a loud growl but is pretty much harmless. $bearCerms = Bear::create(array( 'name' => 'Cerms', 'type' => 'Black', 'danger_level' => 4 )); // bear 3 is named Adobot. He is a polar bear. He drinks vodka. $bearAdobot = Bear::create(array( 'name' => 'Adobot', 'type' => 'Polar', 'danger_level' => 3 )); $this->command->info('The bears are alive!'); // seed our fish table
536

Answer

---- // our fish wont have names... because theyre going to be eaten // we will use the variables we used to create the bears to get their id Fish::create(array( 'weight' => 5, 'bear_id' => $bearLawly->id )); Fish::create(array( 'weight' => 12, 'bear_id' => $bearCerms->id )); Fish::create(array( 'weight' => 4, 'bear_id' => $bearAdobot->id )); $this->command->info('They are eating fish!'); // seed our trees table
372

Answer

- Tree::create(array( 'type' => 'Redwood', 'age' => 500, 'bear_id' => $bearLawly->id )); Tree::create(array( 'type' => 'Oak', 'age' => 400, 'bear_id' => $bearLawly->id )); $this->command->info('Climb bears! Be free!'); // seed our picnics table
587

Answer

- // we will create one picnic and apply all bears to this one picnic $picnicYellowstone = Picnic::create(array( 'name' => 'Yellowstone', 'taste_level' => 6 )); $picnicGrandCanyon = Picnic::create(array( 'name' => 'Grand Canyon', 'taste_level' => 5 )); // link our bears to picnics
307

Answer

- // for our purposes we'll just add all bears to both picnics for our many to many relationship $bearLawly->picnics()->attach($picnicYellowstone->id); $bearLawly->picnics()->attach($picnicGrandCanyon->id); $bearCerms->picnics()->attach($picnicYellowstone->id); $bearCerms->picnics()->attach($picnicGrandCanyon->id); $bearAdobot->picnics()->attach($picnicYellowstone->id); $bearAdobot->picnics()->attach($picnicGrandCanyon->id); $this->command->info('They are terrorizing picnics!'); } }
743

Answer

Solution:

Make sure yourBear model extendsIlluminate\Database\Eloquent\Model like this:

class Bear extends \Illuminate\Database\Eloquent\Model

otherwiseBear model won't havecreate method and you will get error that this method doesn't exist

People are also looking for solutions to the problem: How to clean a PHP multidimensional associative array to map with javascript es6

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.