解决-BadMethodCallException调用未定义的方法App \ Post :: tags()

时间:2019-07-12 17:21:52

标签: php laravel many-to-many

我正在处理我的Laravel项目,并且存在多对多关系的问题:无法使用“同步”功能将数据存储在中介表中。

我正在按照本系列教程中的内容进行操作:Part 37 - Adding Tag UI/UX

此代码行似乎存在问题:$post->tags()->sync($request->tags, false);

它抛出错误:

  

BadMethodCallException调用未定义的方法App \ Post :: tags()

我尝试使用attach函数代替sync,这不起作用。

我不知道代码的哪一部分可能导致此问题。 请告诉我你们是否注意到了什么。暴躁!

Post.php(模型)

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(模型)

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(迁移-中介表)

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(视图-选择多个标签)

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

1 个答案:

答案 0 :(得分:2)

您已在Post模型中将关系定义为标记,但您正在调用标记。您应该将其更改为标签,因为它是一个AboutToMany关系。

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