Php 拉维尔多对多关系

Php 拉维尔多对多关系,php,laravel,eloquent,many-to-many,Php,Laravel,Eloquent,Many To Many,我面临一个问题。我是新来的。我在我的应用程序中创建了多对多关系,但显示为错误。请检查以下代码: 错误 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null (SQL: insert into `category_post` (`category_id`, `created_at`, `post_id`, `updated_at`) values (1, 2019-11-09 17

我面临一个问题。我是新来的。我在我的应用程序中创建了多对多关系,但显示为错误。请检查以下代码:

错误

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null (SQL: insert into `category_post` (`category_id`, `created_at`, `post_id`, `updated_at`) values (1, 2019-11-09 17:00:29, ?, 2019-11-09 17:00:29))
发布型号代码:

public function  categories()
{
    return $this->belongsToMany('App\Category')->withTimestamps();
}
类别型号代码

public function posts()
{
    return $this->belongsToMany('App\Post')->withTimestamps();
}
 public function store(Request $request)
{
    $this->validate($request,[


    ]);

    $image = $request->file('image');
    $slug = str_slug($request->name);

    if(isset($image))
    {
        $imageName = $slug.'-'.time().uniqid().'.'.$image->getClientOriginalExtension();

        if(!Storage::disk('public')->exists('posts/'))
        {
            Storage::disk('public')->makeDirectory('posts/');
        }
        $postImage = Image::make($image)->resize(1600,1066)->stream();

        Storage::disk('public')->put('posts/'.$imageName,$postImage);
    }else{

        $imageName = 'default.png';
    }



    $posts = new Post();

    $posts->user_id =Auth::id();

    $posts->title = $request->title;
    $posts->slug = $slug;
    $posts->body = $request->body;
    $posts->image = $imageName;

    $posts->categories()->attach($request->categories);
    $posts->tags()->attach($request->tags);

    $posts->is_approved = true;
    if(isset($request->status))
    {
        $posts->status = true;
    }else{

        $posts->status = false;
    }



    $posts->save();

  //  $posts->tags()->attach($request->tags);
    Toastr::success('Post Is created successfully','Success');
    return redirect()->route('admin.posts.index');

}
后置控制器代码

public function posts()
{
    return $this->belongsToMany('App\Post')->withTimestamps();
}
 public function store(Request $request)
{
    $this->validate($request,[


    ]);

    $image = $request->file('image');
    $slug = str_slug($request->name);

    if(isset($image))
    {
        $imageName = $slug.'-'.time().uniqid().'.'.$image->getClientOriginalExtension();

        if(!Storage::disk('public')->exists('posts/'))
        {
            Storage::disk('public')->makeDirectory('posts/');
        }
        $postImage = Image::make($image)->resize(1600,1066)->stream();

        Storage::disk('public')->put('posts/'.$imageName,$postImage);
    }else{

        $imageName = 'default.png';
    }



    $posts = new Post();

    $posts->user_id =Auth::id();

    $posts->title = $request->title;
    $posts->slug = $slug;
    $posts->body = $request->body;
    $posts->image = $imageName;

    $posts->categories()->attach($request->categories);
    $posts->tags()->attach($request->tags);

    $posts->is_approved = true;
    if(isset($request->status))
    {
        $posts->status = true;
    }else{

        $posts->status = false;
    }



    $posts->save();

  //  $posts->tags()->attach($request->tags);
    Toastr::success('Post Is created successfully','Success');
    return redirect()->route('admin.posts.index');

}

我的create.blde.php文件代码正常,所有请求都正常,但在我提交我的帖子时出现上述错误。提前感谢您有一个
属于帖子和类别之间的许多
关系。为了在透视表上创建该关系,您必须具有
category\u id
post\u id

代码中的这一行:

 $posts->categories()->attach($request->categories);
是正确的,但您正在尝试在保存对象之前将类别附加到post对象。因此,未保存的post对象还没有
id

保存您的帖子,然后使用您已经编写的
attach()
方法附加类别

您的
标签()和附件也是一样的。保存post对象后将其插入