Search code examples
laravel-4lazy-loadingeloquenteager-loadingrelationships

Laravel 4.1 eager loading 3 deep nested relationships will return undefined index on third relationship


I will be doubting thomas as much as possible. Sorry for long codes.

The problem is going to be presented using series, books, authors, authorPicture

Series Model

/**
 * The Series Model
 */

namespace My\Project\Series\Eloquent;

use Illuminate\Database\Eloquent\Model;

class Series Extends Model
{
    protected $table = "series";

    protected static $bookModel = "My\Project\Books\Eloquent\Book";

    public function books()
    {
        return $this->hasMany(static::$bookModel);
    }
}

Book Model

/**
 * The Book Model
 */
namespace My\Projects\Books\Eloquent;

use Illuminate\Database\Eloquent\Model;

class Book Extends Model
{
    protected static $seriesModel = "My\Project\Series\Eloquent\Series";

    protected static $authorModel = "My\Project\Authors\Eloquent\Author";

    public function series()
    {
        return $this->belongsTo(static::seriesModel);
    }

    public function authors()
    {
        return $this->hasMany(static::$authorModel);
    }
}

Author Model

/**
 * The Author Model
 */

namespace My\Project\Authors\Eloquent;

use Illuminate\Database\Eloquent\Model;

class Author extends Model
{
    protected static bookModel = "My\Project\Books\Eloquent\Book";

    protected static authorPictureModel = "My\Project\AuthorPictures\Eloquent\AuthorPicture";

    public function authorPicture()
    {
        return $this->hasOne(static::$authorPictureModel);
    }

    public function book()
    {
        return $this->belongsToMany(static::$bookModel);
    }

}

Author Picture Model

/**
 * The Author Picture Model
 */

namespace My\Project\AuthorPictures\Eloquent;

use Illuminate\Database\Eloquent\Model;

class AuthorPicture
{
    protected static $authorModel = "My\Project\Authors\Eloquent\Author";

    public function author()
    {
        return $this->belongsTo(static::$authorModel);
    }
}

Series Service

/**
 * Series Service
 */

namespace My\Project\Series;

use My\Project\Series\SeriesProviderInterface;

class SeriesService 
{
    protected $seriesProvider;

    public function __construct(SeriesProviderInterface $seriesProvider)
    {
        $this->seriesProvider = $seriesProvider;
    }

    public function findSeriesById($id)
    {
        $series = $this->seriesProvider->findById($id); // Will return model

        return $series->with('books.authors.authorPicture')->get()->toArray();
    }
}

Here comes the problem. The print_r result of SeriesFacade::findSeriesById($id) will include books and authors but not authorPicture.

The wierd thing; if I cancel return and dump DB::getQueryLog(), I can see that, a query was fired to find authorPictures in (.,.,.).

I can already associate models on save too. But I get undefined index if I try to eager load these nested relationships.


I read all the questions and even opened an issue on github laravel/laravel, but still not sure if I am doing something wrong or something else. That is why I ask this question.


Solution

  • The query was being executed and the data was not being presented because;

    the authorPicture relation was not set under $visible array of authors. That was not a bug. That was a feature I couldn't mastermind.