Php Laravel 5调用未定义的方法Illumb\Database\Eloquent\Collection::attach()

Php Laravel 5调用未定义的方法Illumb\Database\Eloquent\Collection::attach(),php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我正在使用具有多对多关系的laravel 5。 下面是我的代码 Article.php namespace App; use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; class Article extends Model { protected $fillable = ['title', 'body', 'published_at', 'user_id']; protected $dates = ['published

我正在使用具有多对多关系的laravel 5。 下面是我的代码

Article.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Article extends Model {

protected $fillable = ['title', 'body', 'published_at', 'user_id'];

protected $dates = ['published_at'];

public function setPublishedArAttribute($date)
{
    Carbon::createFromFormat('Y-m-d', $date);
    $this->attributes['published_at'] = Carbon::parse($date);
}

public function scopePublished($query)
{
    $query->where('published_at', '<=', Carbon::now());
}

public function scopeUnpublished($query)
{
    $query->where('published_at', '>', Carbon::now());
}

public function user()
{
    return $this->belongsTo('App\User');
}

public function tags()
{
    return $this->belongsToMany('App\Tag');
}

}
ArticleController.php

namespace App\Http\Controllers;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Request;
use Carbon\Carbon;
use Auth;

class ArticlesController extends Controller {

public function __construct()
{
    $this->middleware('auth', ['except', 'index']);
}

public function index()
{
    $articles = Article::latest('published_at')->published()->get();
    return view('articles.index', compact('articles'));
}

public function show(Article $article)
{
    return view('articles.show', compact('article'));
}

public function create()
{
    if(Auth::guest()){
        return redirect('articles');
    }
    $tags = \App\Tag::lists('name', 'id');
    return view('articles.create', compact('tags'));
}

public function store(Requests\ArticleRequest $request)
{   
    $article = Auth::user()->articles()->create($request->all());
    $article->tags->attach($request->input('tags'));
    \Session::flash('flash_message', 'Your article has been created!');
    Article::create($request->all());
    return redirect('articles');
}

public function edit(Article $article)
{
    return view('articles.edit', compact('article'));
}

public function update(Article $article, Requests\ArticleRequest $request)
{       
    $article->update($request->all());
    return redirect('articles');
}

}
当我尝试创建文章并使用ArticleController中的store函数将其存储在数据库中时,我得到以下错误

ArticlesController.php行中的FatalErrorException:调用未定义的方法Illumb\Database\Eloquent\Collection::attach()

我已经设置了文章和标签之间的多对多关系,还适当地添加了迁移

但当我尝试使用标记添加文章时,我得到了一个错误。 让我知道我的代码有什么问题。设置关系时缺少任何内容

这也是迁移表文件

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->timestamps();
    });

    Schema::create('article_tag', function(Blueprint $table){
        $table->increments('id');
        $table->integer('article_id')->unsigned()->index();
        $table->foreign('article_id')->references('id')->on('articles')->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('article_tag');
}

}

让我知道如何使attach与多对多关系一起工作

使用关系属性(
$article->tags
)将返回与文章相关的所有标记的集合。此
集合
没有
attach()
方法

$article->tags()->attach($request->input('tags'));
您需要使用关系方法(
$article->tags()
)。relationship方法返回relationship实例,该实例具有
attach()
方法

$article->tags()->attach($request->input('tags'));

您需要调用
$article->tags()
方法,而不是
$article->tags
属性

在Laravel中,以属性名的形式调用关系方法,神奇地将相关模型作为集合获取。换句话说,这两行在Laravel中的意思完全相同:

$tags = $article->tags;
以及:

另一方面,调用relationship方法返回一个关系实例,在您的例子中-
belongToMany
attach()
方法是
belongTomany
类的一部分

$tags = $article->tags()->get();