Laravel 5调用未定义的方法IlluminateDatabaseEloquentCollection:


Laravel 5 Call to undefined method IlluminateDatabaseEloquentCollection::attach()

我使用的是具有多对多关系的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');
}
}

Tag.php

namespace App;
use Illuminate'Database'Eloquent'Model;
class Tag extends Model {
protected $fillable = [
    'name'
];
public function articles()
{
    return $this->belongsToMany('App'Article');
}
}

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行中的致命错误异常:调用未定义的方法Illuminate''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');
}
}

让我知道我应该怎么做才能使多对多关系的附加工作

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

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

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

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

在Laravel中,以属性名称的形式调用关系方法,可以神奇地将相关模型作为Collection。换言之,这两条线在Laravel中的意思完全相同:

$tags = $article->tags;

和:

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

另一方面,调用relationship方法会返回一个relationship实例,在您的例子中是BelongsToManyattach()方法是BelongsToMany类的一部分。