Ad

BadMethodCallException Call To Undefined Method App\Post::tags()

- 1 answer

Im working on my Laravel project, and have a problem with many-to-many relationship : cannot use "sync" function to store the data in Intermediary Table.

Im following the tutorial in this series : Part 37 - Adding Tag UI/UX

Problem seems to be with this code line : $post->tags()->sync($request->tags, false);

It throws back the error :

BadMethodCallException Call to undefined method App\Post::tags()

I have tried to use attach function instead of sync, does not work.

I dont know which part of code could lead to this issue. Pls tell me if u guys notice anythings. Tysm !

Post.php (Model)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = "posts";

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

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

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

Tag.php (Model)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    protected $table = "tags";

    public function post(){
        return $this->belongsToMany('App\Post', 'post_tag');
    }
}

create_post_tag_table.php (migrations - Intermediary Table)

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostTagTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('post_tag', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('post_id')->unsigned()->nullable();
            $table->foreign('post_id')->references('id')->on('posts');
            $table->bigInteger('tag_id')->unsigned()->nullable();
            $table->foreign('tag_id')->references('id')->on('tags');
        });
    }
}

posts.create.blade.php (views - select multiple tags)

<select class="form-control select2-multi" name="tags[]" multiple="multiple" style="width:100%;">
    @foreach($tags as $tag)
        <option value='{{ $tag->id }}'>{{ $tag->name }}</option>
    @endforeach
</select>

PostsController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Post;
use App\Tag;
use App\User;

class PostsController extends Controller
{
    public function create()
    {
        $tags = Tag::all();
        return view('posts.create')->with('tags', $tags);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'category_id' => 'required',
        ]);

        $post = new Post;
        $post->title = $request->input('title');
        $post->description = $request->input('description');
        $post->content = $request->input('content');
        $post->category_id = $request->input('category_id');
        $post->user_id = auth()->user()->id;
        $post->status = $request->input('status');

        $post->save();

        $post->tags()->sync($request->tags, false);

        return redirect('/posts')->with('success', 'Post created.');
    }
}
Ad

Answer

You have defined the relationship as tag in your Post model but you are calling tags. You should change it to tags since it is a belongsToMany relationship.

public function tags() 
{
    return $this->belongsToMany('App\Tag', 'post_tag');
}
Ad
source: stackoverflow.com
Ad