2017-07-04 3 views
1

Rückkehr habe ich eine Tabelle wie folgt:Warum ist mein Laravel Morphmap leer

Schema::create('comments', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('user_id'); 
     $table->integer('comment_id'); 
     $table->string('comment_type'); 
     $table->text('comment'); 
     $table->timestamps(); 
    }); 

, die wie so bevölkert ist:

INSERT INTO `job`.`comments` (`id`, `user_id`, `comment_id`, `comment_type`, `comment`, `created_at`, `updated_at`) VALUES ('1', '1', '8', 'courses', 'Hello', NULL, NULL); 

ich ein Kursmodell wie so haben:

<?php 

namespace App\Modules\Courses\Models; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Support\Facades\Auth; 
use Laravelista\Comments\Comments\Traits\Comments; 

class Courses extends Model 
{ 
    use Comments; 

    protected $table = 'courses'; 

    public function comments() 
    { 
     return $this->morphMany('App\Modules\Comments\Models\Comments', 'comment'); 
    } 

, die mein Kommentarmodell mit meiner Morph-Karte verwandelt:

<?php 

namespace App\Modules\Comments\Models; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Database\Eloquent\Relations\Relation; 

Relation::morphMap([ 
    'courses' => App\Modules\Courses\Models\Courses, 
]); 

class Comments extends Model 
{ 
    public function commentable() 
    { 
     return $this->morphTo(); 
    } 
} 

Wenn ich meine Karte in meiner Reihe und Verwendung Kursen an der Art Referenz gibt es leer, wie Sie von Tinker sehen:

enter image description here

Aber wenn ich den vollständigen Namensraum im comment_type in der Datenbank funktioniert es:

INSERT INTO `job`.`comments` (`id`, `user_id`, `comment_id`, `comment_type`, `comment`, `created_at`, `updated_at`) VALUES ('1', '1', '8', 'App\\Modules\\Courses\\Models\\Courses', 'Hello', NULL, NULL); 

enter image description here

Was mache ich falsch, und wie kann ich dieses Problem beheben?

edit: Ich habe auch versucht, die Vorschläge von zu ihm in meinen App Service-Provider platzieren, aber das weder funktioniert nicht:

enter image description here

Antwort

0

Versuchen Sie, die Beziehung registrieren :: morphMap() in ein Service Provider

AppServiceProvider

use Illuminate\Database\Eloquent\Relations\Relation; 
... 
public function boot() 
{ 
    Relation::morphMap([ 
     'courses' => 'App\Modules\Courses\Models\Courses', 
    ]); 
} 

https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations

+0

haben versucht, das auch und immer noch die gleichen http://i.imgur.com/z929bBe.png –

+0

@randommman versuchen, diese Beziehung :: morphMap ([ 'Kurse' => Kurse :: Klasse, ]) ; –

+0

Leider immer noch das gleiche –