2016-07-27 8 views
1

Ich habe ein Problem beim Abrufen von Modell mit injizierten Eloquent in Controller-Methode - es findet kein Modell mit ID in URL. Ich erinnere, dass ich in einem anderen ControllerEloquent Modellinjektion in die Methode gibt null zurück

geändert
DB::setFetchMode(PDO::FETCH_ASSOC); 

dd ($ Infrastruktur) liefert nur Metadaten über Modell:

Infrastructure {#257 ▼ 
#table: "infrastructures" 
#fillable: array:10 [▶] 
#connection: null 
#primaryKey: "id" 
.... 
} 

Meine Controller-Methode:

public function show(Infrastructure $infrastructure) 
{ 

    $card = []; 
    $card['name'] = $infrastructure->name; 
    $card['id'] = $infrastructure->id; 
    $card['type'] = "infrastructure"; 
    $card['items']['manufacturer'] = $infrastructure->manufacturer; 
    $card['items']['type'] = $infrastructure->infrastructuretype()->first()- >name; 
    $card['items']['kind'] = $infrastructure->kind()->first()->name; 
    $card['items']['serial'] = $infrastructure->serial; 
    $card['items']['more info'] = Crypt::decrypt($infrastructure->long_desc); 

    $title = 'Infrastructure details '; 

    $warranty_left = $infrastructure->warranty_date > $infrastructure->purchase_date ? (new Carbon($infrastructure->warranty_date))->diffInDays(new Carbon($infrastructure->purchase_date)) : 0; 

    return view('infrastructure.show', compact('infrastructure', 'card', 'title')); 
} 

Meine Routen:

Route::model('infrastructure', 'App\Infrastructure'); 


Route::group(['middleware' => 'auth'], function() { 
Route::resource('infrastructure', 'InfrastructureController'); 
get('infrastructure/{infrastructure}/logs', [ 
    'uses' => '[email protected]' 
]); 
resource('infrastructuretype', 'InfrastructureTypeController'); 


Route::get('auth/logout', 'Auth\[email protected]'); 

});

Antwort

0

Ich habe keine Ahnung, warum das funktioniert nicht mehr, aber ruuning

php artisan route:clear 
php artisan route:cache 

gearbeitet.

0

Ändern Sie in Ihren Routen die Datei Route::model('infrastructure', 'App\Infrastructure'); in Route::bind('infrastructure', 'App\Infrastructure');.

+0

nein, immer noch das gleiche –

0

Ihre Route :: Ressource definiert den Platzhalter mit Pluralnamen, der den Platzhalter 'Infrastrukturen' bilden würde.

Sie binden das Platzhalterzeichen 'Infrastruktur' an das Modell, das nur für Ihre showInfrastructureLog-Methode funktionieren würde.

Sie diesen Befehl in der Konsole verwenden können, jede Ressource vollständigen Pfad zu zeigen:

php artisan route:list 

Sie können dies leicht zu beheben, indem

Ändern
Route::model('infrastructure', 'App\Infrastructure'); 

Um

Route::model('infrastructure', 'App\Infrastructure'); 

(I habe das Route :: Model nie persönlich für Model Binding benutzt, immer vom RouteServiceProvider, aber ich gehe davon aus, dass es funktioniert)

ändern sich auch die Platzhalter in der 'Infrastruktur/{Infrastruktur}/logs' auf 'Infrastrukturen'

+0

Ich habe nicht verstanden, was zu was ändern. Du hast zu denselben Zeilen geschrieben. –

+1

Route :: Modell ("Infrastruktur", "App \ Infrastruktur"); hätte der zweite sein sollen, mein Schlechter – Shipupi