简体   繁体   English

laravel 多对多关系新

[英]laravel many to many relation new

i am facing a problem.我面临一个问题。 i am new in laravel.我是 laravel 的新手。 i created many to many relation in my application.but it's shown error.我在我的应用程序中创建了多对多关系。但它显示错误。 please check bellow code:请检查以下代码:

Error错误

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))

Post Model code:发布 Model 代码:

public function  categories()
{
    return $this->belongsToMany('App\Category')->withTimestamps();
}

Category model code类别 model 代码

public function posts()
{
    return $this->belongsToMany('App\Post')->withTimestamps();
}

PostController Code后控制器代码

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

}

My create.blde.php file code is okey all request goes appropriately but above error shown when i submit my post..Thank in advance我的 create.blde.php 文件代码正常,所有请求都正常进行,但在我提交帖子时显示上述错误..提前致谢

You have a belongs to many relationship between posts and categories .您在帖子类别之间有一个belongs to many的关系。 In order to create that relationship on your pivot table, you must have a category_id and a post_id .为了在您的 pivot 表上创建该关系,您必须有一个category_id和一个post_id

This line in your code:您的代码中的这一行:

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

is correct, but you are trying to attach the categories to the post object before you have saved it.是正确的,但您在保存之前尝试将类别附加到帖子object。 Thus, the unsaved post object has no id yet.因此,未保存的帖子object 还没有id

Save your post , and then attach the categories with the attach() method you have already written.保存您的帖子,然后使用您已经编写的attach()方法附加类别。

Same thing with your tags() attachment.您的tags()附件也是如此。 Stick it in after you have saved the post object.保存帖子object 后将其粘贴。

Save the post model object before you attach other models在附加其他型号之前保存帖子 model object

$posts = new Post();

$posts->user_id = auth()->id();
$posts->title = $request->title;
$posts->slug = $slug;
$posts->body = $request->body;
$posts->image = $imageName;
$posts->is_approved = true;
$posts->status = $request->status ? true : false;

$posts->save();

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

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM