Laravel - Eager Laden Polymorphe Relation Verwandte Modelle

Kann ich begierig laden polymorphe Beziehungen/Modelle, ohne jede n+1-Problemen. Jedoch, wenn ich versuche, auf ein Modell bezogen auf das polymorphe Modell, das n+1 problem angezeigt und ich kann nicht scheinen zu finden, a fix. Hier ist das genaue setup, um es zu sehen vor Ort:

1) DB-Tabelle, die Namen/Daten

history

Laravel - Eager Laden Polymorphe Relation Verwandte Modelle

companies

Laravel - Eager Laden Polymorphe Relation Verwandte Modelle

products

Laravel - Eager Laden Polymorphe Relation Verwandte Modelle

services

Laravel - Eager Laden Polymorphe Relation Verwandte Modelle

2) Modelle

//History
class History extends Eloquent {
    protected $table = 'history';

    public function historable(){
        return $this->morphTo();
    }
}

//Company
class Company extends Eloquent {
    protected $table = 'companies';

    //each company has many products
    public function products() {
        return $this->hasMany('Product');
    }

    //each company has many services
    public function services() {
        return $this->hasMany('Service');
    }
}

//Product
class Product extends Eloquent {
    //each product belongs to a company
    public function company() {
        return $this->belongsTo('Company');
    }

    public function history() {
        return $this->morphMany('History', 'historable');
    }
}

//Service
class Service extends Eloquent {
    //each service belongs to a company
    public function company() {
        return $this->belongsTo('Company');
    }

    public function history() {
        return $this->morphMany('History', 'historable');
    }
}

3) Routing

Route::get('/history', function(){
    $histories = History::with('historable')->get();
    return View::make('historyTemplate', compact('histories'));
});

4) Vorlage mit n+1 angemeldet nur becacuse der $Geschichte->historable->company->name, Kommentar, n+1 geht Weg.. aber wir brauchen eine entfernte Verwandte name des Unternehmens:

@foreach($histories as $history)
    <p>
        <u>{{ $history->historable->company->name }}</u>
        {{ $history->historable->name }}: {{ $history->historable->status }}
    </p>
@endforeach
{{ dd(DB::getQueryLog()); }}

Ich muss in der Lage sein zu laden, den Namen des Unternehmens mit Spannung (in einer Abfrage), da es ein Verwandtes Modell, die polymorphe Beziehung Modelle Product und Service.
Ich habe schon seit Tagen, aber finde keine Lösung.
History::with('historable.company')->get() ignoriert company im historable.company.
Was wäre eine effiziente Lösung für dieses problem sein?

InformationsquelleAutor der Frage Wonka | 2014-11-04

Schreibe einen Kommentar