BadMethodCallException,带有消息“调用未定义的方法Illuminate \ Database \ Query \ Builder :: belongToMany()" [英] BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::belongToMany()'

查看:180
本文介绍了BadMethodCallException,带有消息“调用未定义的方法Illuminate \ Database \ Query \ Builder :: belongToMany()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照此Laracast中的说明进行操作:

Following the instructions in this laracast :

https://laracasts.com/series/laravel-5-fundamentals/情节/21

我创建了一个渠道模型

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('App\Tag'); //tag_id
    }
}

和标记模型

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

因此,数据透视表中的Channel和Tag之间存在多对多关系.

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

我的迁移情况如下

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

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');
    }
}

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

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=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 * FROM channel_tag');
=> []
>>> $channel->tags()->attach(1);
=> null
>>> DB::select('SELECT * FROM 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 with message 'Call to undefined method Illuminate\Database\Query\Builder::belongToMany()'

这没有意义,感觉可能是一个错误,但我不确定.我正在使用Laravel Framework版本5.2.6和PHP 5.6.16

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

推荐答案

belongsToMany不是belongToMany

public function channels()
{
    return $this->belongsToMany('App\Channel'); //channel_id
}

这篇关于BadMethodCallException,带有消息“调用未定义的方法Illuminate \ Database \ Query \ Builder :: belongToMany()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆