简体   繁体   English

如何在 Laravel 5.8 中创建搜索

[英]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.我想按帖子“slug”的标题创建搜索,当它搜索帖子时,我想在列表中显示结果。 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注意:我在 header.blade.php 上有搜索字段,并访问此页眉页到母版页,并在 listing.blade.php 上加载该母版页

This is the search code on header.blade.php page这是header.blade.php页面上的搜索代码

<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它是 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它是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.您没有给出 slug 与帖子之间的关系。 Are the slugs in a different table ? slug 是否在不同的表中? 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.我不知道,比如说,slug 有一个单独的表,从 slug 到帖子是一对多的关系。 That is one slug may has many posts.那是一个蛞蝓可能有很多帖子。 And, slug has a field named, name for slug text.并且,slug 有一个字段, name为 slug 文本。

From your given form, it indicates HomepageController@index .从您给定的表单中,它表示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.在方法内部编写查询以获取 slugs 表中 slugs 文本匹配的帖子。 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 表有一个 slug 列,则可以按如下方式替换查询,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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