Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent
Last updated 7 months ago.
0

Did you make a proper table for the comments?

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateCommentsTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id')->unsigned();

            $table->text('body');

            $table->integer('commentable_id')->unsigned();
            $table->string('commentable_type');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('comments');
    }
}
Last updated 7 months ago.
0

Yes, I have. Pretty much exactly like what you have, but with some extra fields.

Last updated 7 months ago.
0

In addition to @XoneFobic answer, there is a Schema method to create them:

$table->morphs('taggable'); 	Adds INTEGER taggable_id and STRING taggable_type
Last updated 7 months ago.
0

I don't think there is a problem with the schema. I think something is going wrong with the relations.

Here is my schema anyway: http://laravel.io/bin/G8J

Here the full code to my models:
Article
Comment

All the other relations works just fine, it's just the polymorphic relations that just don't work. As far as I can see it's exactly the same as shown in the documentation, but with different names. I have quadruple checked the names and they are correct.

Edit: Figured it out

So embarrasing.. I simply forgot to put return infront of $this->morphMany('Comment', 'commentable'); in my models. God damnit. Thanks for trying to help me anyway, always appreciate a helpful community :-)

Last updated 7 months ago.
0

Haha, I also overlooked it when I was looking over your code.

Next to the forum, we also use an IRC channel which is pretty active. Feel free to join to ask for help or help other people :)

Last updated 7 months ago.
0

Im using this Polymorphic relations but it is returning class not found error.

namespace MyProject;

class MyFolder extends Model {
    protected $table = 'my_folder';

    public function storage() {
        return $this->morphMany('\MyProject\Storage', 'owner');
    }
}
namespace MyProject;

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

It is returning a Class 'MyFolder' not found.

I tried $this->morphMany('MyProject\Storage', 'owner'); but still its not working.

Please help. Thanks!

0

edanao said:

Im using this Polymorphic relations but it is returning class not found error.

namespace MyProject;

class MyFolder extends Model {
   protected $table = 'my_folder';

   public function storage() {
       return $this->morphMany('\MyProject\Storage', 'owner');
   }
}
namespace MyProject;

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

It is returning a Class 'MyFolder' not found.

I tried $this->morphMany('MyProject\Storage', 'owner'); but still its not working.

Please help. Thanks!

try composer dump-autoload

0

arafatx said:

edanao said:

Im using this Polymorphic relations but it is returning class not found error.

namespace MyProject;

class MyFolder extends Model {
   protected $table = 'my_folder';

   public function storage() {
       return $this->morphMany('\MyProject\Storage', 'owner');
   }
}
namespace MyProject;

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

It is returning a Class 'MyFolder' not found.

I tried $this->morphMany('MyProject\Storage', 'owner'); but still its not working.

Please help. Thanks!

try composer dump-autoload

@edanao create aliases for your models in config/app.php reference

0

check if you are storing commentable_type in databse only model name and if you do this, you can have this message "Class 'MyFolder' not found" because you need to store complete namespace Namespace\Entities\MyFolder .

Last updated 6 years ago.
0

Sign in to participate in this thread!

Cloudways

Your banner here too?

mengidd mengidd Joined 1 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.