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

andreixfr's avatar

Tag system - Search by tag at click Laravel

I want to make a search function by tags. When a tag is clicked, to show me all the content with the value from tag ( like model, tag1, tag2 etc. ). For that, I make a page and tested if this can displays what I need. With ->where('tag', '=', 'model') I got all posts with `tag = model'. But now, I need to display based on what tag is clicked. So, here is what I have in single-news : enter image description here

These tags are generated by this code:

<div class="container">
            <label>Tags

              <a href="{{ url('') }}">
                  <input class="input-tags" type="text" data-role="tagsinput" value="{{ $news->tag }}" disabled>
              </a>


              </label>
            </div>

        </div>

Here is my controller at the moment:

public function newsTag()
        {

                $data = $this->data;



                $data['news_details'] = News::with('category_name')->where('locale','=',Session::get('locale'))->where('tag','=','model')->orderBy('created_at', 'desc')->get();


                $data['categories'] = Category::with(['news' => function ($q){
                        $q->orderBy('title', 'asc');}])->where('category','=','news')->where('locale','=',Session::get('locale'))->get();

                // $data['categories'] = Category::with('news')->where('category','=','news')->where('locale','=',Session::get('locale'))->get();

                $data['instanews_details'] = InstagramNews::where('locale','=',Session::get('locale'))->where('category','=','news')->get();

                // $displayitem = DisplayItem::where('slug','=',url('project'))->first();

                // $data['page'] = $displayitem;
                // date_default_timezone_set('Asia/Kolkata');
                // // return $date = date('Y-m-d H:i:s');
                // return $date = strtotime('-1 hour');
                // echo 'John visited last ' . $date;

                $data['title'] = trans('fab.news');;

                $data['breadcrumb'] = 'news';

                // $data['menu'] = $tag->title;

                $data['metadescription'] = "news";

                $data['metatitle'] = 'news';

                // $data['blogs'] = $tag->blog;

                return view('news.news_tag',$data);

        }

Here is my route:

Route::get('news_tag', 'ModelController@newsTag');

Here is my page for results - news_tag.blade.php:

@extends('layout.front.template')
@section('content')

<!-- Profile talent section -->
   <section class="vc_row wpb_row vc_row-fluid modificartion" style="overflow: hidden;">
   <div class="container news-container">
      <div id="news-rows" class="row">
          <div class="wpb_column vc_column_container vc_col-sm-12">
              <div class="vc_column-inner ">
                  <div class="wpb_wrapper">
                     <section class="gallery-element isotop padding-item none-padding hidden-s hidden-sm hidden-xs">
                        <div class="container">
                           <div class="header-element">
                               <h3 id="newstxt" class="title-element font-disolve">@lang('fab.news')</h3>
                           </div>
                           <div class="content-tab">
                               <div id="options" class="clearfix">
                                  <a href="javascript:void(0)">
                                   <ul id="filters" class="option-set clearfix inline-mainb" data-option-key="filter" data-toggle="modal" data-target="#myModal">
                                      @lang('fab.newsletter')
                                   </ul>
                                 </a>
                               </div>

                               <div class="row" style="margin-right:0px;bottom: 40px; position: relative">

                                 <div class="col-md-3 contentWrap" id="column1" style="padding-right: 0px;">
                                   <ul class="scroller3 content">
                                     @foreach($news_details as $key => $news)
                                         <!-- <ul class="scroller1"> -->


                                             <a href="{{ url('news') }}/{{ $news->slug }}">
                                               <div class="element transition {{ $news->category_name->slug }} editorial news video-exhibition" style="width: 100%">
                                                 <div class="item-gallery {{ $news->news_class }}">
                                                   <div class="content-item">
                                                       <img src="{{ url('images/news') }}/{{ $news->image }}"/>
                                                      <div class="text">
                                                          <div class="name">{{ $news->category_name->title }}</div>
                                                          <div class="content-gallery-grid">
                                                             <h3 class="title">
                                                               <!-- <a href="{{ url('news') }}/{{ $news->slug }}"> -->
                                                                 {{ $news->title }}
                                                               <!-- </a> -->
                                                             </h3>
                                                          </div>
                                                      </div>
                                                   </div>
                                                 </div>
                                              </div>
                                             </a>


                                     @endforeach
                                   </ul>
                                 </div><!-- 1st col -->
                               </div>

@stop

So, to conclude, I need to make this search function based of the tags from single news of every news. So, If news with id 1 has tags - test, tag, me, phone, and I press on tag, I want to be redirected to news_tag page, and to get only posts with tag on column tag from table News. enter image description here

0 likes
1 reply

Please or to participate in this conversation.