我有我的Laravel项目的搜索框,它似乎工作,因为请求后我会得到http://project.dev/search?q=fifth
但没有在刀片模板中打印。Laravel搜索不打印结果
这里是我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' => '[email protected]', '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个职位与结果我得到的第一页标题结果是这样的:
但是当我去到第二页一切都变了!
为什么?
withQuery似乎在Laravel中不存在。 – manian
@manian即使删除该部分仍然没有。 –
你引用了两个视图,但只给出了一个代码? – Scopey