[英]Laravel Search results
我有以下问题? 当您在我们的平台上搜索foo时,您将获得一个空白页的搜索结果。 如果用户输入的搜索查询不在我们的平台上,并且响应如“抱歉,但我们的平台上没有foo”这样的小小的响应,我想输入一个搜索结果。
在我们的searchcontroller.php中的控制器下,我有以下内容
<?php
class SearchController extends BaseController
{
public function getIndex()
{
$search = Request::get('q');
if (empty($search)) {
return Redirect::to('gallery');
}
$extends = explode(' ', $search);
$images = Images::where('title', 'LIKE', '%' . $search . '%')
->orWhere('tags', 'LIKE', '%' . $search . '%')->orWhere('category', '=', $search)
->where('deleted_at', '=', NULL)->where('approved','=',DB::raw(1))->orderBy('created_at', 'desc');
foreach ($extends as $extend) {
$images->orWhere('tags', 'LIKE', '%' . $extend . '%')
->orWhere('title', 'LIKE', '%' . $search . '%')
->orWhere('image_description', 'LIKE', '%' . $search . '%');
}
$images = $images->paginate(30);
return View::make('gallery/index')
->with('images', $images)
->with('title', t('Searching for').' "' . ucfirst($search) . '"');
}
}
在您看来,您将执行以下操作:
@if ($images->isEmpty())
Sorry but we found no search results.
@else
@foreach ($images as $image)
<img src="{{ $image }}">
@endforeach
@endif
那是伪代码,然后是更多。 基本上只检查Paginator
实例是否有任何项目,如果没有,则显示“无搜索结果”消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.