2
votes

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.

2
I'm having a very similar issue. Have you been able to resolve this?mhopkins321
Unfortunately, no. But as an alternative solution; I created methods in my providers for threads and created a seperate array. Using eager loading helps me to save queries as well. (Not sure about single responsibility thing) (Check: github.com/laravel/laravel/issues/2646)Hilmi Erdem KEREN

2 Answers

1
votes

I had a similar problem, which I solved. First, I submitted a query as follows (all the relationships had been set up correctly)

$batch = Batch::with('batchItem.teamMember.employee.employeeType')->find($id);

Then I got one of the batch items

$batchItem = $batch->BatchItem[0];

But when I print_r($batchItem) the last relation employeeType did not show up. Even more disconcerting, when I attempted to get data from $batchItem, for instance

$batchItem->teamMember->employee->name

It would issue more queries, as if no data was eager loaded at all!

The solution was sublime. It appears Eloquent is extremely case sensitive...

$batchItem = $batch->batchItem[0];

solved all of the issues!

Not sure if this is your problem, but perhaps it might help.

0
votes

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.