Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kikismedia's avatar

How Can I build a hashtag feature like twitter

is there any best way to create a hashtag feature on laravel

0 likes
3 replies
Snapey's avatar

you have an input text area and someone types #something

orest's avatar
orest
Best Answer
Level 13

@kikismedia

The general idea would be to something like

The tables

hashtags
- id
- title
tweets
- id
- body
hashtag_tweets
- id 
- hashtag_id
- tweet_id

The relationships

class Tweet
{
     public function hashtags()
     {
              return $this->belongsToMany(Hashtag::class);
     }
}

class Hashtag
{
     public function tweets()
     {
              return $this->belongsToMany(Tweet::class);
     }
}

And of course as @snapey suggested, you need to label everything that comes after the symbol # as a hashtag which can be done with regular expressions.

Please or to participate in this conversation.