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');
}
}
Yes, I have. Pretty much exactly like what you have, but with some extra fields.
In addition to @XoneFobic answer, there is a Schema method to create them:
$table->morphs('taggable'); Adds INTEGER taggable_id and STRING taggable_type
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.
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 :-)
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 :)
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!
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
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
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 .
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community