3
votes

I am using laravel 5 with many to many relationship. Below is my code.

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

}

When I try to create article and store it in database using store function in ArticleController I get following error.

FatalErrorException in ArticlesController.php line : Call to undefined method Illuminate\Database\Eloquent\Collection::attach()

I have set many to many relationship between articles and tags, also properly added migration.

But when I try to add article using tag I get error. Let me know what is wrong with my code. Is anything missing in setting relationship.

Also this is migration table file

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

}

Let me know what should I do to make attach work with many to many relationship

2

2 Answers

0
votes

Using the relationship attribute ($article->tags) will return a Collection of all the tags related to the article. This Collection does not have an attach() method.

You need to use the relationship method ($article->tags()). The relationship method returns the relationship instance, which does have the attach() method.

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

You need to call $article->tags() method instead of $article->tags property.

In Laravel, calling a relationship method in shape of property name, magically gets related models as a Collection. In other words, those two lines mean exactly the same thing in Laravel:

$tags = $article->tags;

And:

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

Calling relationship method, on the other hand, returns a Relationship instance, in your case - BelongsToMany. attach() method is part of BelongsToMany class.