title: How to make like and unlike in Symfony 4?
body: <p>I'm designing like and functionality in my project and I'm stuck to display the icon.</p>
  
  <p>User.php</p>
  
  <pre><code>/**
   * @ORM\ManyToMany(targetEntity="App\Entity\Image", inversedBy="users")
   */
  private $likes;
  
  public function __construct()
  {
      $this-&gt;images = new ArrayCollection();
      $this-&gt;likes = new ArrayCollection();
  }
   ....
   /**
   * @return Collection|Image[]
   */
  public function getLikes(): Collection
  {
      return $this-&gt;likes;
  }
  
  public function addLike(Image $like): self
  {
      if (!$this-&gt;likes-&gt;contains($like)) {
          $this-&gt;likes[] = $like;
      }
  
      return $this;
  }
  
  public function removeLike(Image $like): self
  {
      if ($this-&gt;likes-&gt;contains($like)) {
          $this-&gt;likes-&gt;removeElement($like);
      }
  
      return $this;
  }
  </code></pre>
  
  <p>Image.php</p>
  
  <pre><code>/**
   * @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="likes")
   */
  private $users;
  
  public function __construct()
  {
      $this-&gt;likes = new ArrayCollection();
      $this-&gt;users = new ArrayCollection();
  }
  ...
  /**
   * @return Collection|User[]
   */
  public function getUsers(): Collection
  {
      return $this-&gt;users;
  }
  
  public function addUser(User $user): self
  {
      if (!$this-&gt;users-&gt;contains($user)) {
          $this-&gt;users[] = $user;
          $user-&gt;addLike($this);
      }
  
      return $this;
  }
  
  public function removeUser(User $user): self
  {
      if ($this-&gt;users-&gt;contains($user)) {
          $this-&gt;users-&gt;removeElement($user);
          $user-&gt;removeLike($this);
      }
  
      return $this;
  }
  </code></pre>
  
  <p>Twig</p>
  
  <pre><code>          &lt;a href="{{ path('like',{'id':image.id}) }}"&gt;
              {% if  user likes the image display this %}  
                &lt;i id="like" class="{{ unlike }} text-danger"&gt;&lt;/i&gt;&amp;nbsp;{{ image.users.count  }} 
  
                {% else show this %}
                       &lt;i id="like" class="{{ like }} text-danger"&gt;&lt;/i&gt;&amp;nbsp;{{ image.users.count  }} 
                {% endif %}
                &lt;/a&gt;
  </code></pre>
  
  <p>User repository</p>
  
  <pre><code>public function findByLikes($value): ?User
  {
      return $this-&gt;createQueryBuilder('l')
          -&gt;leftJoin('l.likes', 'u')
          -&gt;leftJoin('u.user','e')
          -&gt;addSelect('u','e')
          -&gt;where('l.id = :val')
          -&gt;setParameter('val', $value)
          -&gt;getQuery()
          -&gt;getOneOrNullResult()
      ;
  }
  </code></pre>
  
  <p>Can anyone help me with the code? I think there is no issue with controller. May be I'm not able to write logic in twig.
  So my intentions is to display the icon for the liked and unliked. I have used single icon for both like and unlike. If the user click on like icon that should flip to unlike icon and vice versa.</p>