Post tags sync with additional data like slug.

Published 3 weeks ago by nanadjei2

Am building a blog post with tags. On the tags table the tags are posting correctly but its additional slug are not posting.

This is the controller

  $post->fill([
                'staff_id'  => Auth::user()->id,
                'meta_descripition' => $request->meta_descripition,
                'title' => $request->title,
                'content' => $request->content,
                'image' => $imageFileName,
                $date = str_replace('.', '-', $request->input('published_at')),
                'published_at' => Carbon::parse($date)->format('Y-m-d'),
                'slug' => str_replace(' ', '-', strtolower($request->slug))
            ])->save();

$data =  ['slug' => $request->slug];

   $post->tags()->sync($request->post_category_tags, $data);

This is my CmsPost model

  public function tags() {
             return $this->belongsToMany('App\CmsPostCategoryTag', 'cms_post_category_tags', 
               'cms_post_id', 'cms_post_category_id');
       }
Best Answer (As Selected By nanadjei2)
rumm.an

when you sync multiple relations you need to pass an associative array as an first argument. Say, I want to attach tags with id 1,2,3 along with the pivot data slug as some-slug for each of the record:

$data = ['slug' => 'some-slug']
$post->tags()->sync([1 => $data, 2 =>$data, 3=>$data]);

but in your case you are doing wrong way.

$tags = [];
$pivot = ['slug' => $request->slug];
foreach($request->post_category_tags as $tagId) {
    $tags[$tagId] = $pivot;
}
$post->tags()->sync($tags);

This should work for you!

rumm.an

How you want it(slugs) to work?

nanadjei2

yes sir

rumm.an

I mean how? What behaviour you want? What is your database relationships structure?

nanadjei2

this is how my database looks like:

   id | cms_post | cms_post_category_id | slug | created_at | updated_at 
rumm.an

and what about tags and the pivot table ?

nanadjei2

And this is how it happends when I post:

   id | cms_post | cms_post_category_id | slug | created_at | updated_at 
   -------------------------------------------------------------------------------------------
    1   |     1     |          1          |  NULL |     NULL  |      NULL 
    2   |     2     |          3          |  NULL |     NULL  |      NULL 
rumm.an

$post->tags()->sync($request->post_category_tags, $data);

this suggests that you have a column slug in your pivot table. Are you getting any errors with this code?

nanadjei2

Yes the pivot table is the cms_post_category_tags table. I have termed the actual tags as cms_post_categories

nanadjei2

I dnt have any errors with $post->tags()->sync($request->post_category_tags, $data); but the slug and the timestamps won't post.

nanadjei2

Yes i have published_at on the posts table

nanadjei2

My only problem is that on the pivot table the slug and the timestamps does not post.

rumm.an

okay got it!

rumm.an

when you sync multiple relations you need to pass an associative array as an first argument. Say, I want to attach tags with id 1,2,3 along with the pivot data slug as some-slug for each of the record:

$data = ['slug' => 'some-slug']
$post->tags()->sync([1 => $data, 2 =>$data, 3=>$data]);

but in your case you are doing wrong way.

$tags = [];
$pivot = ['slug' => $request->slug];
foreach($request->post_category_tags as $tagId) {
    $tags[$tagId] = $pivot;
}
$post->tags()->sync($tags);

This should work for you!

nanadjei2

Damn...! Man it worked. But it's left with the timestamps

rumm.an

If you want your pivot table to have automatically maintained created_at and updated_at timestamps, use the withTimestamps method on the relationship definition:

See docs

public function tags() {
    return $this->belongsToMany(
        'App\CmsPostCategoryTag', 'cms_post_category_tags', 
        'cms_post_id', 'cms_post_category_id'
    )->withTimestamps();
}

Please sign in or create an account to participate in this conversation.