方法Illuminate \ Database \ Query \ Builder :: languages不存在

时间:2019-05-17 05:10:11

标签: php laravel artisan

我正在尝试将languages表与posts表一起附加。我已经用language_postpost_id列建立了一个language_id表。当我尝试用语言添加帖子时,如上所示,它给了我这个错误。

已选中App/Language.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Language extends Model
{
    public function posts()
    {
        return $this->belongsToMany('App\Post')->withTimestamps();
    }
}
?>

PostController的方法中,public function store(Request $request)是我收到此错误的地方

//above this all fields working perfectly
 $post->save();
        $post->languages()->attach($request->languages);

1 个答案:

答案 0 :(得分:2)

我想您忘了对Post模型做同样的事情:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function languages()
    {
        return $this->belongsToMany('App\Language')->withTimestamps();
    }
}