Skip to content

Instantly share code, notes, and snippets.

@AdrianKuriata
Last active November 1, 2016 10:43
Show Gist options
  • Select an option

  • Save AdrianKuriata/490619b03b9910f15d9bd9f9a9c07100 to your computer and use it in GitHub Desktop.

Select an option

Save AdrianKuriata/490619b03b9910f15d9bd9f9a9c07100 to your computer and use it in GitHub Desktop.

Revisions

  1. AdrianKuriata revised this gist Nov 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tags.php
    Original file line number Diff line number Diff line change
    @@ -29,5 +29,5 @@ public function storePost(Request $request)
    $getTagsId = Tag::whereIn('id', $createdTags)->pluck('id')->toArray();

    //Add exists and not exists tags to one array for save it with attach posts
    $tagsToAdd = array_merge($eTags, $getTagsId);
    $tagsToAdd = array_merge($exTags, $getTagsId);
    }
  2. AdrianKuriata renamed this gist Nov 1, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. AdrianKuriata created this gist Nov 1, 2016.
    33 changes: 33 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    public function storePost(Request $request)
    {
    $notexistsTags = array();
    $existsTags = Tag::whereIn('id', $request->input('tag_name'))->get();

    foreach($request->input('tag_name') as $tag_name) {
    // Check if tag currently exist in database
    foreach($existsTags as $tag) {
    $match = false;
    if($tag_name == $tag->id) {
    $match = true;
    break;
    }
    }
    // If tag from user's input was not matched to tag in database
    // add it to the $notexistsTags array.
    if(!$match) {
    $notexistsTags['tag_name'] = $tag_name;
    }
    }

    //Get a pluck ID exists tags
    $exTags = $existsTags->pluck('id')->toArray();

    //Create not exists tags
    $createdTags = Tag::create($notexistsTags);

    //Get ID tags which was created
    $getTagsId = Tag::whereIn('id', $createdTags)->pluck('id')->toArray();

    //Add exists and not exists tags to one array for save it with attach posts
    $tagsToAdd = array_merge($eTags, $getTagsId);
    }