Alexis
Alexis

Reputation: 149

Laravel Call to undefined method create() and save()

I'm trying to seed my DB but I have a problem when I use modelName::create() or $modelName->save(). I have this kind of error

{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Call to undefined method Doc::save()","file":"/Applications/MAMP/htdocs/Doc_project/app/database/seeds/DatabaseSeeder.php","line":45}

or

Call to undefined method Doc::create()

but i dont know why.

My Model :

class Doc extends Eloquent  {

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'doc';

  protected $fillable = array('creators_name', 'type_document', 'title', 'description');

  public function steps(){
    return this->hasMany('Step')
  }

  public function tags(){
    return this->belongsToMany('Tag', 'doc_tag', 'id_doc', 'id_tag')
  }
}

My Seeder :

class DatabaseSeeder extends Seeder {

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

        $this->call('DocAppSeeder');
        $this->command->info('Doc app seeds finished.'); // show information in the command lin }
    }
}

class DocAppSeeder extends Seeder {

    public function run(){

        DB::table('doc')->delete();
        DB::table('step')->delete();
        DB::table('tag')->delete();
        DB::table('doc_tag')->delete();

    /*  DB::table('doc')->insert(array(
            'creators_name' => 'Alexis',
            'type_document' => 'Solution',
            'title'                 => 'NoAD Printer',
            'description'       => 'Installation imprimante en NoAd'
        ));*/

        $doc = new Doc;
        $doc->creators_name = 'Alexis';
        $doc->type_document = 'Solution';
        $doc->title = 'NoAD Printer';
        $doc->description = 'Installation imprimante en NoAd';
        $doc->save();
        /*$docTest = Doc::create(array(
            'creators_name' => 'Alexis',
            'type_document' => 'Solution',
            'title'                 => 'NoAD Printer',
            'description'       => 'Installation imprimante en NoAd'
        ));
}
}

I try with DB::Table(...)->insert(...)it works, but I can't use it because I need to get some information on each object

Someone have an idea? Thank you

Upvotes: 1

Views: 7579

Answers (2)

Khan Aqib
Khan Aqib

Reputation: 41

Check your migration file -

Rollback the migration, create a fresh version of migration file and proceed.

It usually happens with artisans when used to create a migration.

For me, i rollback the migration and recrested the migration with another name and it works.

Upvotes: 1

Alana Storm
Alana Storm

Reputation: 166166

It looks like PHP is using a different global level Doc class than the one you think it is. Maybe for a facade or alias (self link, contains details instructions on how to debug facade issues)?

Regardless, the best course of action is to see where PHP thinks this class is. In you seeder, right before save, include the following debugging code

$r = new ReflectionClass('Doc');
var_dump(
    $r->getFilename()
);     
var_dump(
    $r->getName()
);

This will tell you the full-name of the class (if it's an alias) and where the class is defined. My guess is the class you think is Doc is not your Eloquent model, and is, in fact, a different class.

Based on your comments below, it sounds like someone's defined a class named Doc in

/Applications/MAMP/htdocs/Doc_project/app/database/migrations/2014_10_12_201016‌​_doc.php

This isn't normal -- although it's easy to see how it might have happened. You probably used the artisan command to create a migration like this

php artisan migrate:make doc

By not using the more explicate form

php artisan migrate:make create_doc_table

You inadvertently create a migration class with the same name as your model. Try recreating this migration with a less specific name, and you should be all set.

Upvotes: 2

Related Questions