【问题标题】:how to create search in laravel 5.8如何在 laravel 5.8 中创建搜索
【发布时间】:2020-03-29 20:16:24
【问题描述】:

我想按帖子“slug”的标题创建搜索,当它搜索帖子时,我想在列表中显示结果。我已经为作者和类别创建了列表页面。当我点击类别时,它会显示当前点击的类别和帖子,对于作者来说是一样的。现在我只想创建一个搜索。当搜索某些东西时,结果显示我的帖子带有一个列表,就像我为作者和类别所做的那样。

注意:我在 header.blade.php 上有搜索字段,并访问了这个标题页到母版页,并在listing.blade.php 上加载了那个母版页

这是 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>

是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'));
    }

}

它是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>

这是我的路线页面

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');

【问题讨论】:

    标签: php laravel laravel-5.8


    【解决方案1】:

    您没有给出 slug 与帖子之间的关系。蛞蝓在不同的桌子上吗?是一对一还是一对多的关系!!!

    我不知道,比如说,slug 有一个单独的表,从 slug 到帖子是一对多的关系。那就是一个蛞蝓可能有很多职位。而且,slug 有一个名为 name 的字段,用于 slug 文本。

    从您给定的表格中,它表示HomepageController@index。或者您可以根据需要使用单独的控制器和方法。

    在方法内部编写查询以获取 slugs 表中 slugs 文本匹配的帖子。喜欢下面,

    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
    }
    

    或者,如果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); 
    

    【讨论】:

    • 然后进行第二个查询。尝试一下,或者根据您的需要更新它以查看返回的数据格式。
    • 我在 ListingPageController 中添加了搜索方法,现在我想在搜索帖子时将我作为帖子列表
    • 好的,然后使用路由路径从表单中调用你的搜索方法,在你的搜索方法中使用查询并返回查询结果
    • 它不起作用,好像刷新页面一样
    • 在您的表单操作中给出正确的路由路径。我想你知道从动作属性调用路由路径的语法......
    猜你喜欢
    • 2016-08-06
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多