jeudi 23 novembre 2017

Laravel 5 How to do save new tags to database during store the post

I want to create new tags when I creating the post. I will use old ones and new ones but I cant figure out how to do save new tags before the sync.

Create Blade - Tags Section (For Posts)

<select class="form-control m-select2" id="m_select2_11" multiple name="tags[]">
   <optgroup label="Etiketler">
       @foreach($tags as $tag)
          <option value=""></option>
       @endforeach
   </optgroup>
 </select>

And PostController

public function store(StoreBlogPost $request)
{

    $posts = new Post;
    $posts->title = $request->title;
    $posts->slug = str_slug($request->title);
    $posts->caption = $request->caption;
    $posts->content = $request->input('content');

    if ($request->tags) 
    {
        foreach ($request->tags as $tag) 
        {
         Tag::firstOrCreate(['tag_name' => $tag]);
        }
    }

    $posts->save();
    $posts->categories()->sync($request->categories, false);
    $posts->tags()->sync($request->tags,false);
    return redirect()->route('posts.index');
}

I know it looks like weird but I am beginner, so.. Any advice ?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2n0BDqX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire