lundi 25 juillet 2016

Laravel 5 Call to undefined method Illuminate\Database\Eloquent\Collection::attach()

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire