0
votes

I want to insert tags in Tags table as well as post_tag table from post creating page. When i create a new post tags are inserting in Tags table but not inserting in post_tag tablbe. it is showing Invalid datetime format.

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'start,tas' for column anusondhan.post_tag.tag_id at row 1 (SQL: insert into post_tag (post_id, tag_id) values (20, start,tas))


            //Store Post
            public function storeNewPost(Request $request){
                //return $request->all();
                $request->validate([
                    'post_title' => 'required',
                    'post_details' => 'required',
                    'category_id' => 'required',
                    'image' => 'image|max:15360|dimensions:max_width=4000,max_height=3000'
                ]);

                $image = $request->file('post_thumbnail');
                
                $name_gen=uniqid().'.'.$image->getClientOriginalExtension();
                Image::make($image)->resize(700,400)->save('frontend/assets/images/post/'.$name_gen);
                $save_url = 'frontend/assets/images/post/'.$name_gen;

                $post = Post::create([
                    'user_id' => Auth::id(),
                    'post_uper_title' =>$request->post_uper_title,
                    'post_title' =>$request->post_title,
                    'post_sub_title' =>$request->post_sub_title,
                    'post_details' =>$request->post_details,
                    'post_slug' =>str_replace(' ', '-', $request->post_title),
                    'seo_title' =>$request->seo_title,
                    'seo_descp' =>$request->seo_descp,
                    'lead' =>$request->lead,
                    'lead2' =>$request->lead2,
                    'featured' =>$request->featured,
                    'repoter_name' =>$request->repoter_name,
                    'division_id' =>$request->division_id,
                    'district_id' =>$request->district_id,
                    'category_id' =>$request->category_id,
                    'post_thumbnail' =>$save_url,
                    'thumbnail_caption' =>$request->thumbnail_caption,
                    'thumbnail_alt' =>$request->thumbnail_alt,
                    'created_at' => Carbon::now(),
                ]);

                if($post){
                    $tags = explode(",", implode($request->tags));
                    $tagNames = [];
                    if (!empty($tags)) {
                        foreach ($tags as $tagName)
                        {
                        $tag = Tags::firstOrCreate(['name'=>$tagName]);
                        if($tag)
                        {
                            $tagNames[] = $tag->id;
                        }
                        }
                        
                    }
                    $post->tags()->sync($request->tags);
                    $notification = array(
                        'message' => 'Post Inserted Successfully',
                        'alert-type' => 'success'
                    );
                    return redirect()->route('all.posts')->with($notification);
                }else{
                    return back();
                }             

            }//end insert post

** Here is my Tags table **


        public function up()
        {
            Schema::create('tags', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('slug')->nullable();
                $table->longText('description')->nullable();
                $table->longText('tag_thumb')->nullable();
                $table->text('thumb_caption')->nullable();
                $table->text('thumb_alt')->nullable();
                $table->softDeletes();
                $table->timestamps();
            });
        }


** Here is my Post_tag Table **


        public function up()
        {
            Schema::create('post_tag', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedBigInteger('post_id')->nullable();
                $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
                $table->unsignedBigInteger('tag_id')->nullable();
                $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
                $table->timestamps();
            });
        }


1

1 Answers

0
votes

I think you need to use $tagNames array instead of $request->tags when syncing:

$post->tags()->sync($tagNames);

As per the Laravel documentation, the sync() method expects an array of IDs: https://laravel.com/docs/8.x/eloquent-relationships#syncing-associations