Laravel方法同步不存在

Laravel方法同步不存在,laravel,synchronization,Laravel,Synchronization,后模型 <?php namespace App\Model\User; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function tags(){ return $this->belongsToMany('App\Model\User\Tag','post__tags', 'post_id', 'tag_id'); } public functio

后模型

<?php

namespace App\Model\User;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
   public function tags(){
     return $this->belongsToMany('App\Model\User\Tag','post__tags', 'post_id', 'tag_id');
  }

  public function categories() {
     return $this->belongsToMany('App\Model\User\Category','category__posts','post_id', 'category_id');
  }
}

Sync是builder实例上的一种方法,您正在集合中使用它

 // change your code like this
 $post->tags()->sync($request->tags);
 $post->categories()->sync($request->categories);

您需要使用以下各项:

$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
不要忘记标记和类别中的()

$post->categories without()是一个集合实例

$post->categories()是一个belongTomany实例

可能与
public funcion store()
{
// do validation here
   try{
      $post = new Post;
      $post->title = $request->title;
      $post->subtitle  =  $request->subtitle;
      $post->slug  =  $request->slug;
      $post->body  =  $request->body;           
      $post->status  =  1;                   
      $post->save();     // This works correct 
      $post->tags->sync($request->tags); // Not working
      $post->categories->sync($request->categories);  // Not working
   }
   catch(\Exception $e){ 
      return redirect(route('post.index'))->with('message', 'Error Adding Post into system!'.$e->getMessage()); // getting Message "Method sync does not exist. "
    }      
}
 // change your code like this
 $post->tags()->sync($request->tags);
 $post->categories()->sync($request->categories);
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);