[英]Laravel search not print the results
I have search box for my Laravel project it seems working because after request I'll get http://project.dev/search?q=fifth
but nothing prints in blade template. 我有我的Laravel项目的搜索框似乎有效,因为在请求之后我会得到http://project.dev/search?q=fifth
但在刀片模板中没有打印。
here is my SearchController
这是我的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 !');
}
}
Here is my search form 这是我的搜索表单
<!-- 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 -->
Here is my routes 这是我的路线
Route::any('/search', ['uses' => 'SearchController@index', 'as' => 'search.index']);
Here is my blade 这是我的刀片
@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
where is my mistake? 我的错在哪里?
Update 更新
Now I have search box which is work and show data with pagination but when i search for example for word sample
which i have 3 posts with that title in first page of results I'm getting results like this: 现在我有搜索框是工作和显示分页的数据,但当我搜索单词sample
,我有3个帖子在结果的第一页有该标题我得到这样的结果:
But when i go to second page everything changes! 但是当我进入第二页时,一切都在变化!
Why? 为什么?
Ok guys i got it thanks, the issue was withQuery method as i used compact
i shouldn't use with
i just added it to `compact and now it's working. 玉家伙我得到它的感谢,这个问题是withQuery方法为我所用compact
我不应该使用with
我只是把它加入到`紧凑,现在它的工作。 but what about counting the results? 但计算结果怎么样? how can i pass the number of founded result in blade? 如何在刀片中传递创建结果的数量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.