display more than one tag on views

Published 7 months ago by helpmyworld

Good day, how do I display all selected tags to show in my posts views. When I dd, I see that I have more than one tag but on the view, it shows only one, the first tag.

my view

 <dl class="dl-horizontal">
                            <dt>Tags:</dt>
                            <dd>
                                @foreach($post->tags as $tag)
                                    <span class="label label-default"><a href="{{route('tags.show',
                                  $tag->id)}}" style="color:white;">{{$tag->name}}</a></span>
                                @endforeach
                            </dd>
                        </dl>

My post and tag models are ok.

What else should I show you to help?

Best Answer (As Selected By helpmyworld)
helpmyworld

I found a solution, it turns out I did not put "[]" in my code.



<select class="form-control select2-multi" name="tags[]" id="tags"  multiple="multiple">
                                        @foreach($tags as $tag)
                                            <option value="{{$tag->id}}">{{$tag->name}}</option>
                                        @endforeach
                                    </select>
Dry7
Dry7
7 months ago (151,680 XP)

@helpmyworld check the html source code, maybe the reason in css.

wilk_randall

Try changing your <a> tag to something like this:


<a href="{{ route('tags.show', ['id' => $tag->id]) }}" style="color:white;">{{ $tag->name }}</a>

This might be what is causing only one tag to show up. In a named route, you need to pass your route parameters in through an array as the second parameter. If your route parameter for id is named something different, you would use that instead. https://laravel.com/docs/5.5/routing#named-routes

robrogers3

just out of curiosity what does dd($post->tags) show in the controller?

helpmyworld

@robrogers my dd($post->tags) show that I have 3 tags or 2 tags in a case where I did select more than one tag.

for example i have included more than one tag here

    #parameters: array:6 [▼
      "_token" => "GeUaA8OGx6gKQ2deAa8ePqJQtF2Mm36dAqT3umkP"
      "title" => "CSS IS FINE1"
      "slug" => "CSS-IS-FINE1"
      "tags" => "4"
      "categories" => "1"
      "body" => "<h2 style="box-sizing: border-box; font-family: 'Open Sans', sans-serif; font-weight: 500; line-height: 1.1; color: #333333; margin-top: 20px; margin-bottom: 10 ▶"
    ]
  }```
helpmyworld

@wilk_randall your method works, however, still getting the same results on my views.

@Dry7 I am carefully checking my code and css.

Thank you.

robrogers3

umm, shouldn't dd($post->tags) look like this in your browser:

Collection {#414 ▼
  #items: array:3 [▼
    0 => Tag {#428 ▶}
    1 => Tag {#431 ▶}
    2 => Tag {#432 ▶}
  ]
}
helpmyworld

@robrogers3 if you may, where do i do the dd($post->tags in the controller? Not trying to be funny or anything but i am learning as i work.

Cronix
Cronix
7 months ago (481,780 XP)

Right after you get the posts and assign them to $post

wing5wong

Did you define a post -> tags relationship on your model? it shouldnt return a 4 here, but a collection

 #parameters: array:6 [▼
      
      "title" => "CSS IS FINE1"
      "slug" => "CSS-IS-FINE1"
      "tags" => "4"
      

you only see one tag because you have one single value "4"

helpmyworld

I found a solution, it turns out I did not put "[]" in my code.



<select class="form-control select2-multi" name="tags[]" id="tags"  multiple="multiple">
                                        @foreach($tags as $tag)
                                            <option value="{{$tag->id}}">{{$tag->name}}</option>
                                        @endforeach
                                    </select>

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