我想通过帖子的标题“ 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');
您没有给出与帖子之间的关系。 the在其他桌子上吗?是一对一还是一对多的关系!!!>
正如我所不知道的,有一个单独的表来记录,这是从到职位的一对多关系。那一个可能有很多职位。并且,Slug有一个名为name
的字段,用于输入Slug文本。
在给定的表格中,它表示HomepageController@index
。或者,您可以根据需要使用单独的控制器和方法。
在方法内部编写查询以获取在表名表中表头文本匹配的帖子。就像下面一样,
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表具有子栏列,则可以按以下方式替换查询,
$posts = Post::with(['comments','category','creator']) ->where('slug', 'LIKE', '%'.$request->search. '%') ->where('status',1)->where('created_by',$id) ->orderBy('id','DESC')->paginate(5);