php - BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::belongToMany()' -
following instructions in laracast :
https://laracasts.com/series/laravel-5-fundamentals/episodes/21
i created channel model
class channel extends model { // protected $fillable = [ 'title', 'description', 'published_at', ]; public function scopepublished($query) { $query->where('published_at', '<=', carbon::now()); } public function setpublishedatattribute($date) { $this->attributes['published_at'] = carbon::createfromformat('y-m-d', $date); } /* * tags associated given channel * */ public function tags() { return $this->belongstomany('app\tag'); //tag_id } } and tag model
class tag extends model { // protected $fillable = [ 'name', 'description', ]; /** * channels associated given tag */ public function channels() { return $this->belongtomany('app\channel'); //channel_id } } so there's many-to-many relation betweeen channel , tag through pivot table.
my migrations follows
use illuminate\database\schema\blueprint; use illuminate\database\migrations\migration; class createchannelstable extends migration { /** * run migrations. * * @return void */ public function up() { schema::create('channels', function (blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('description'); $table->text('url'); $table->text('channelposter'); $table->timestamp('published_at'); $table->timestamps(); }); } /** * reverse migrations. * * @return void */ public function down() { schema::drop('channels'); } } and
use illuminate\database\schema\blueprint; use illuminate\database\migrations\migration; class createtagstable extends migration { /** * run migrations. * * @return void */ public function up() { schema::create('tags', function (blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('description'); $table->timestamps(); }); //channel_tag schema::create('channel_tag', function(blueprint $table) { $table->integer('channel_id')->unsigned()->index(); $table->foreign('channel_id')->references('id')->on('channels')->ondelete('cascade'); $table->integer('tag_id')->unsigned()->index(); $table->foreign('tag_id')->references('id')->on('tags')->ondelete('cascade'); $table->timestamps(); }); } /** * reverse migrations. * * @return void */ public function down() { schema::drop('tags'); schema::drop('channel_tag'); } } however, when use artisan tinker connect channel tag follows :
==> php artisan tinker psy shell v0.6.1 (php 5.6.16 — cli) justin hileman >>> $channel=app\channel::first(); => app\channel {#660 id: 1, title: "test1", description: "test1", url: "", channelposter: "", published_at: "2016-01-06 02:54:20", created_at: "2016-01-06 02:54:20", updated_at: "2016-01-06 02:54:20", } >>> $tag = new app\tag; => app\tag {#649} >>> $tag->name = "recommended"; => "recommended" >>> $tag->description = "recommended"; => "recommended" >>> $tag->save(); => true >>> db::select('select * channel_tag'); => [] >>> $channel->tags()->attach(1); => null >>> db::select('select * channel_tag'); => [ {#658 +"channel_id": 1, +"tag_id": 1, +"created_at": "2016-01-05 21:56:46", +"updated_at": "0000-00-00 00:00:00", }, ] >>> $tag->channels->toarray(); badmethodcallexception message 'call undefined method illuminate\database\query\builder::belongtomany()' this not making sense , feels might bug i'm not sure. i'm using laravel framework version 5.2.6 , php 5.6.16
belongstomany not belongtomany
public function channels() { return $this->belongstomany('app\channel'); //channel_id }
Comments
Post a Comment