简体   繁体   中英

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.

here is my 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:

first page

But when i go to second page everything changes!

second page

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. but what about counting the results? how can i pass the number of founded result in blade?

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