Question

I have a Seeder for different models and one of them is giving me the following error when I try to artisan db:seed

PHP Fatal error:  Call to undefined method Illuminate\Events\Dispatcher::create() in C:\www\site\bootstrap\compiled.php on line 3155

Here's Event.php, the model that seems to cause the problem:

<?php

class Event extends Eloquent {

    protected $fillable = array('name', 'played_on');
    protected $table = 'events';

    // RELATIONSHIPS ----------------------------
    // Event has many Decks
    public function decks() {
        return $this->belongsToMany('Deck', 'decks_events', 'event_id', 'deck_id');
    }

}

Note: I added the protected $table = 'events' to try and see if this was causing the problem, I don't think it's required. Here is part of the Seeder. The Deck part works fine (in fact, I do see the 'Added some cards to some decks' message because it crashes)

    // Decks
    $deck_1 = Deck::create(array(
        'meta' => 'U/W Control',
        'player' => 'Cookie Monster'
    ));

    $deck_2 = Deck::create(array(
        'meta' => 'RDW',
        'player' => 'Suzy Waterbottle'
    ));

    // All 3 cards in Deck 1
    $card_1->decks()->attach($deck_1->id);
    $card_2->decks()->attach($deck_1->id);
    $card_3->decks()->attach($deck_1->id);
    // 2 cards in Deck 2
    $card_1->decks()->attach($deck_2->id);
    $card_2->decks()->attach($deck_2->id);

    $this->command->info('Added some cards to some decks');

    // Events
    $event_1 = Event::create(array(
        'name' => 'Super Duper Tourney',
        'played_on' => '07/05/2014'
    ));

    $deck_1->events()->attach($event_1->id);
    $deck_2->events()->attach($event_1->id);

    $this->command->info('Added decks to the event');

This leads me to believe that something wrong happens on the $event_1 = Event::create line but I can't figure out what as it looks exactly like the code used for $deck_1... and $deck_2...

Was it helpful?

Solution

Event is a "reserved" word in Laravel. You may have to change it or namespace your class.

So, instead of your Event model class, it is using Laravel Event, which is Illuminate\Events\Dispatcher.

It is actually an Alias we have on app/config/app.php.

Namespacing it:

<?php namespace App;

use Eloquent;

class Event extends Eloquent {

   ...

}

Using it namespaced:

<?php

use App\Event;

class Whatever {

    $event_1 = Event::create(array(
        'name' => 'Super Duper Tourney',
        'played_on' => '07/05/2014'
    ));

}

You may need to

composer dumpautoload

To refresh your autoloaded namespaces and classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top