BadMethodCallException with message ‘Call to undefined method IlluminateDatabaseQueryBuilder::belongToMany()’

Following the instructions in this laracast :

http://ift.tt/1LRcx0L

I created a 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);
}
/*
* Get the tags associated with the given Channel
*
*/
public function tags() {
return $this->belongsToMany('AppTag'); //tag_id
}
}

and a Tag model


class Tag extends Model
{
//
protected $fillable = [
'name', 'description',
];
/**
* Get the channels associated with the given tag
*/
public function channels() {
return $this->belongToMany('AppChannel'); //channel_id
}
}

so there’s a many-to-many relation betweeen Channel and Tag through the pivot table.

My migrations look as follows


use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateChannelsTable extends Migration
{
/**
* Run the 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 the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('channels');
}
}

and


use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateTagsTable extends Migration
{
/**
* Run the 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 the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tags');
Schema::drop('channel_tag');
}
}

However, when I use artisan tinker to connect a channel to a tag as follows :


==> php artisan tinker
Psy Shell v0.6.1 (PHP 5.6.16 — cli) by Justin Hileman
>>> $channel=AppChannel::first();
=> AppChannel {#660
id: 1,
title: "Test1",
description: "Test1",
url: "",
channelposter: "",
published_at: "2022-01-06 02:54:20",
created_at: "2022-01-06 02:54:20",
updated_at: "2022-01-06 02:54:20",
}
>>> $tag = new AppTag;
=> AppTag {#649}
>>> $tag->name = "Recommended";
=> "Recommended"
>>> $tag->description = "Recommended";
=> "Recommended"
>>> $tag->save();
=> true
>>> DB::select('SELECT * FROM channel_tag');
=> []
>>> $channel->tags()->attach(1);
=> null
>>> DB::select('SELECT * FROM channel_tag');
=> [
{#658
+"channel_id": 1,
+"tag_id": 1,
+"created_at": "2022-01-05 21:56:46",
+"updated_at": "0000-00-00 00:00:00",
},
]
>>> $tag->channels->toArray();
BadMethodCallException with message 'Call to undefined method IlluminateDatabaseQueryBuilder::belongToMany()'

This is not making sense and it feels like it might be a bug but I’m not sure. I’m using Laravel Framework version 5.2.6 and PHP 5.6.16

from Newest questions tagged laravel-5 – Stack Overflow http://ift.tt/1Z4qmMR
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.