我的Laravel项目的搜索框似乎有效,因为在请求之后我会得到http://project.dev/search?q=fifth
,但在刀片模板中没有任何内容。
这是我的SearchController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
class SearchController extends Controller
{
public function index() {
$countTodayOrders = Post::whereRaw('Date(created_at) = CURDATE()')->count();
$yesterday_posts = Post::whereRaw('Date(created_at) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)')->count();
$weekly_posts = Post::whereBetween( 'updated_at', [Carbon::today()->startOfWeek(), Carbon::today()->endOfWeek()] )->count();
$monthy_posts = Post::whereRaw('MONTH(created_at) = ?', Carbon::today()->month)->count();
$q = Input::get('q');
$posts = Post::where('title','LIKE','%'.$q.'%')->orWhere('slug','LIKE','%'.$q.'%')->get();
if(count($posts) > 0)
return view('theme.search', compact('posts', 'countTodayOrders', 'yesterday_posts', 'weekly_posts', 'monthy_posts'))->withQuery ( $q );
else return view ('theme.index')->withMessage('No Details found. Try to search again !');
}
}
这是我的搜索表单
<!-- search box -->
<form action="/search" method="get" role="search">
<div class="input-group">
<input type="text" class="form-control" name="q" placeholder="Search users"> <span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</form>
<!-- search box -->
这是我的路线
Route::any('/search', ['uses' => 'SearchController@index', 'as' => 'search.index']);
这是我的刀片
@extends('layout.app')
@section('content')
<div class="col-md-9 technology-left">
<div class="tc-ch wow fadeInDown" data-wow-duration=".8s" data-wow-delay=".2s">
@if(isset($details))
<p> The Search results for your query <b> {{ $query }} </b> are :</p>
<h2>Sample User details</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Post</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{$post->name}}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
</div>
</div>
@endsection
我的错误在哪里?
更新
现在我有搜索框工作并显示分页数据,但是当我搜索单词sample
的示例时,我在结果的第一页中有3个帖子和该标题我得到的结果就像这样:
但是当我进入第二页时,一切都在变化!
为什么?
答案 0 :(得分:0)
好的家伙,我明白了,谢谢,问题是withQuery方法,因为我使用compact
我不应该使用with
我只是将它添加到`compact,现在它正在工作。 但计算结果怎么样?如何在刀片中传递已创建结果的数量?