Roberto
Roberto

Reputation: 1089

Laravel 4 Eloquent one-to-many relations error

Using for the first time the Eloquent and i dont understand whats the problem here..

class TourItem extends Eloquent {
    public function tour()
    {
        return $this->belongsTo('Tour');
    }
}

class Tour extends Eloquent {   
    public function tourItem()
    {
        return $this->hasMany('TourItem');
    }
}

Here im trying to get the tour itens but only return NULL.

    $tour = Tour::where('slug', '=', $postSlug)->where('wrh_id', '=', 1)->first();
    if (!is_null($tour)) {

        var_dump($tour->tourItem);

        foreach ($tour->tourItem as $item) {
            var_dump($item->texto);
        }
    }

DB Schema:

    Schema::create('tours', function($t) {
                $t->increments('id');
                $t->string('titulo');
                $t->string('slug');
                $t->boolean('main');
                $t->integer('wrh_id');
                $t->timestamps();
            });

    Schema::create('tour_items', function($t) {
                $t->increments('id');
                $t->string('titulo');
                $t->text('texto');
                $t->text('saiba_mais');
                $t->integer('tour_id');
                $t->timestamps();
            });

Where is the error??

Thank you Roberto

Upvotes: 0

Views: 592

Answers (1)

Rodrigo Gauzmanf
Rodrigo Gauzmanf

Reputation: 2527

EDIT

I test your migrations and model, and all works without touching anything. I create a repo at github replying your question.

I'm not sure why didn't work for Roberto, but maybe you are doing some mistake in setting your database or migration.

I hope that the code from repository help you, and make sure to follow the instruction of the Readme.

Upvotes: 1

Related Questions