简体   繁体   中英

Laravel Search results

I have the following issue? When you do a search for foo on our platform you get a search result with a blank page. I would like to put a search result if a user type in a search query that is not on our platform with a snarky little response like "Sorry but there are no foo's on our platform".

In our searchcontroller.php under controllers i have the following

<?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) . '"');

}

}

In your view you'd do something like this:

@if ($images->isEmpty())
    Sorry but we found no search results.
@else
    @foreach ($images as $image)
        <img src="{{ $image }}">
    @endforeach
@endif

That's more pseudo code then anything. Basically just check if the Paginator instance has any items, if not, show the "no search results" message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM