GrahamMorbyDev's avatar

Search DB

I feel like i ask so much of this community ! And thank you to all for all your help on this road to learning this wonderful framework! But i have yet another question!

So im trying to do searches on a database , simple search form that returns results to a view

my controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\comics;
use App\Http\Requests;

class QueryController extends Controller
{
 
   public function getIndex(Comics $request)
    {
        $this->validate($request, [
            'search' => 'required'
        ]);

        $search = $request->get('search');

        $comics = Comics::where('title', 'like', "%$search%")
            ->orWhere('body', 'like', "%$search%")
            ->paginate(10)
        ;

        return view('pages.search', compact('comics'));
    }
}

My view for search box

<form action="/search" method="GET" role="search" style="margin-top: 40px;">
            <div class="form-group">
                <div class="col-md-4">
                    <input type="text" class="form-control" name="search" placeholder="Search" value="">
                </div>
                <div class="col-md-3">
                    <button type="submit" class="btn btn-default">Submit</button>
                </div>
            </form>

and my route

Route::get('search', 'QueryController@search');

and lastly my output view

<p>
    {{ $comics->count() }} {{ str_plural('comics', $comics->count()) }} matched your query
</p>

@foreach($comics as $comic)
    <!--- Loop through posts and display... -->
@endforeach

<!-- Pagination links... -->
{!! $comics->appends(['search' => Input::get('search')])->render() !!}

The error im getting is

Not Found

The requested URL /search was not found on this server.

the url out is this http://localhost:8888/search?search=batman+

Thanks so much again

0 likes
10 replies
SaeedPrez's avatar

Where is the "search" function in your controller?

@waltz His whole getIndex() is more or less a search function..

GrahamMorbyDev's avatar

here is my full routes folder

//User System
Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

//home pages
Route::get('/home', 'HomeController@index');
Route::get('/', 'PagesController@welcome');
Route::get('about' , 'PagesController@about');
Route::get('contact' , 'PagesController@contact');
Route::get('show/{id}' , 'PagesController@show');
Route::get('blog' , 'PagesController@blog');
Route::get('showcom/{id}' , 'PagesController@showcom');
Route::get('comiccollection' , 'PagesController@comiccollection');
Route::get('search', 'QueryController@search');

//Blog Back 

Route::resource('articles' , 'ArticlesController');
Route::post('articles/{id}/photos' , 'ArticlesController@addPhoto');

//comicbook back end
Route::resource('comics' , 'ComicsController');
Route::post('comics/{id}/photos' , 'ComicsController@addPhoto');
Route::auth();

//Profile pages
Route::get('/profile', 'ProfileController@profile');

ActiveMonkey's avatar

@SaeedPrez - yes, that's what I have seen :)

However, when you call this:

Route::get('search', 'QueryController@search');

then I'm looking for the "public function search()" in the controller. So just wondering if that function also exists in his controller.

1 like
SaeedPrez's avatar

Oh, now I get what @waltz was saying.. sorry, I know I shouldn't come here before first cup of coffee ☺

Route::get('search', 'QueryController@search');

This route is pointing to your QueryControllers search() method, where you have your code in the getIndex() method

1 like
ActiveMonkey's avatar

@SaeedPrez - hope you had your first cup now! So @Lordgreymaul - there are two solutions:

in your routes file: replace Route::get('search', 'QueryController@search'); with Route::get('search', 'QueryController@getindex');

or

in your controller, replace: public function getIndex(Comics $request) with public function search (Comics $request)

SaeedPrez's avatar

Your original post didn't mention that your Laravel application was installed in a sub folder..

Search this forum and Google,... and good luck.

Please or to participate in this conversation.