简体   繁体   中英

how to create search in laravel 5.8

I want to create search by title of the posts "slug" , and when it searched for posts i want to show me the results in a list. I have already created listing page for authors and categories. When i click on categories it will show current clicked category with posts and for authors are the same thing. Now i just want to create a search. When search for something the result show me posts with a list like i did for authors and categories.

Note: I have search field on header.blade.php and accessed this header page to master page and loaded that master page on listing.blade.php

This is the search code on header.blade.php page

<div class="head-search">

  <form role="form" action="/" method="get">
     <!-- Input Group -->
     <div class="input-group">
       <input type="text" name="search" class="form-control" placeholder="Type Something"> 
       <span class="input-group-btn">
         <button type="submit" class="btn btn-primary">Search</button>
       </span>
     </div>
   </form>

</div>

It is ListingPageController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class ListingPageController extends Controller
{
    public function index(){
      return view('front.listing', ['posts'=>[]]);
    }
    public function listing($id){ 
      $posts = Post::with(['comments','category','creator'])->where('status',1)->where('created_by',$id)->orderBy('id','DESC')->paginate(5); 
      return view('front.listing',compact('posts')); 
    }

    public function listing1($id){ 
      $posts = Post::with(['comments','category','creator'])->where('status',1)->where('category_id',$id)->orderBy('id','DESC')->paginate(5); 
      return view('front.listing',compact('posts'));
    }

}

And it's listing.blade.php


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

<section class="breadcrumb_section">
    <div class="container">
        <div class="row">
        </div>
    </div>
</section>

<div class="container">
<div class="row">
<div class="col-md-8">
    @foreach($posts as $key=>$post)
                @if($key === 0)

<div class="entity_wrapper">
    <div class="entity_title header_purple">
        <h1><a href="{{ url('/category') }}/{{ $post->category_id }}">{{ $post->category->name }}</a></h1>
    </div>
    <!-- entity_title -->

    <div class="entity_thumb">
        <img class="img-responsive" src="{{ asset('post') }}/{{ $post->main_image }} " alt="{{ $post->title }}">
    </div>
    <!-- entity_thumb -->

    <div class="entity_title">
        <a href="{{ url('/details') }}/{{ $post->slug }}" target="_blank"><h3> {{ $post->title }} </a></h3>
    </div>
    <!-- entity_title -->

    <div class="entity_meta">
         by: <a href="{{ url('/author') }}/{{ $post->creator->id }}">{{ $post->creator->name }} </a> , {{$post->created_at->diffForHumans()}}
    </div>
    <!-- entity_meta -->

    <div class="entity_content">
        {{ str_limit( $post->short_description,200,'...' )  }}
    </div>
    <!-- entity_content -->

    <div class="entity_social">

        <span><i class="fas fa-comment"></i><a href="{{ url('/details') }}/{{ $post->slug }}" target="_blank">{{ count($post->comments) }}  Comments</a></span>
    </div>
    <!-- entity_social -->

</div>
<!-- entity_wrapper -->
      @else
    @if($key === 1)
<div class="row">
    @endif <!-- first if will be ended here -->
    <div class="col-md-6" style="min-height: 555px;margin-bottom:2%"> <!-- it's for space top of page ignation-->
        <div class="category_article_body">
            <div class="top_article_img">
                <img class="img-fluid" src="{{ asset('post') }}/{{ $post->list_image }}" alt="{{ $post->title }}">
            </div>
            <!-- top_article_img -->

            <div class="category_article_title">
                <h5>
                    <a href="{{ url('/details') }}/{{ $post->slug }}" target="_blank"> {{ $post->title }} </a>
                </h5>
            </div>
            <!-- category_article_title -->

            <div class="article_date">
               by: <a href="{{ url('/author') }}/{{ $post->creator->id }}">{{ $post->creator->name }} </a> , {{$post->created_at->diffForHumans()}}
            </div>
            <!-- article_date -->

            <div class="category_article_content">
               {{ str_limit( $post->short_description,100,'...' )  }}
            </div>
            <!-- category_article_content -->

            <div class="widget_article_social">

                <span><i class="fas fa-comment"></i><a href="{{ url('/details') }}/{{ $post->slug }}" target="_blank">{{ count($post->comments) }}  Comments</a></span>
            </div>
            <!-- article_social -->

         </div>
        <!-- category_article_body -->

        </div>
    <!-- col-md-6 -->
    @if($loop->last)

    </div>
      @endif
        @endif
         @endforeach


         <div style="margin-left: 40%">
             {{ $posts->links() }}

             </div>

And it's my route page

Route::get('/', 'HomePageController@index'); 
Route::get('/category/{id}', 'ListingPageController@listing1');
Route::get('/author/{id}', 'ListingPageController@listing');
Route::get('/listing', 'ListingPageController@index');
Route::get('/details/{slug}', 'DetailsPageController@index')->name('details');

You didn't give the relation between slug with posts. Are the slugs in a different table ? Is it a one-to-one or one-to-many relationship!!!

As I don't know, say, there is a separate table for slugs and it is one-to-many relationship from slug to posts. That is one slug may has many posts. And, slug has a field named, name for slug text.

From your given form, it indicates HomepageController@index . Or you may use a seprate controller and method as your wish.

Inside the method write the query to get posts where slugs text match in slugs table. like following,

public function search(Request $request){
  $posts = Post::with(['comments','category','creator'])
               ->join('slugs', 'posts.slug_id', '=', 'slugs.id')
               ->where('slugs.name', 'LIKE', '%'.$request->search. '%')
               ->where('posts.status',1)
               ->where('posts.created_by',$id)->orderBy('posts.id','DESC')->paginate(5); 

   // return to your designated page with data
}

Or, if posts table has a slug column, you can replace the query as following,

$posts = Post::with(['comments','category','creator'])
              ->where('slug', 'LIKE', '%'.$request->search. '%')
              ->where('status',1)->where('created_by',$id)
              ->orderBy('id','DESC')->paginate(5); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM