Implementing a search engine in Laravel 5.3 error?

100 Views Asked by At

I am trying to implement a search engine in a website I am working on, and I cannot seem to get it working, it is probably a small error or a silly mistake I am making, any suggestions?

The error I see is this:

Undefined variable: results (View: C:\wamp64\www\webproject\resources\views\search.blade.php)

My view of the search page:

@extends('layouts.master')

@section('content')

<div class="search_charities_engine">


    {!! Form::open(['method'=>'GET','url'=>'charity','class'=>'navbar-form navbar-left','role'=>'search'])  !!}

        <div class="input-group custom-search-form">

            <input type="text" class="form-control" name="search" placeholder="Enter Charity Name...">

            <span class="input-group-btn">

                <button class="btn btn-default-sm" type="submit">

                    <i class="fa fa-search"> Search </i>

                </button>

            </span>

        </div>

    {!! Form::close() !!}

</div>


@foreach($results as $result)

    <article class="charity_info"> 

        <img src="#" style="width: 100px; height: 100px; float: left; margin-right: 25px;">

            <h4> {{ $result->name }} </h4>

            <p> {{ $result->description}} </p>

            <div class="interaction">

                <a href="#"> Favourite </a> |
                <a href="#"> Edit </a> |
                <a href="#"> Donate </a>

            </div>

    </article>

@endforeach

@endSection

My routes (web.php) - the only routes that are relevant in this question are the bottom 2, the second to last one outputs all the charities onto a different page - this works!:

Route::group(['middleware' => ['web']], function()
{
    Route::get('/', function ()
    {
        return view('welcome');
    })->name('home');

    Route::post('/signup', [
        'uses' => 'UserController@postSignUp',
        'as' => 'signup'
    ]);

    Route::post('/signin', [
        'uses' => 'UserController@postSignIn',
        'as' => 'signin'
    ]);

    Route::get('logout', [
        'uses' => 'UserController@getLogout',
        'as' => 'logout'
    ]);

    Route::get('profile', [
        'uses' => 'UserController@profile',
        'as' => 'profile'
    ]);

    Route::post('profile', [
        'uses' => 'UserController@updateAvatar',
        'as' => 'profile'
    ]);

    Route::get('about', [
        'uses' => 'UserController@getAbout',
        'as' => 'about'
    ]);

    Route::get('contact', [
        'uses' => 'UserController@getContact',
        'as' => 'contact'
    ]);

    Route::get('searchPage', [
        'uses' => 'UserController@getSearchPage',
        'as' => 'searchPage'
    ]);

    Route::get('insert', [
        'uses' => 'UserController@getInsert',
        'as' => 'insert'
    ]);

    Route::post('store',[
        'uses' => 'UserController@postStore',
        'as' => 'store'
    ]);

    Route::post('contact', [
        'uses' => 'UserController@postContact',
        'as' => 'contact'
    ]);


    Route::group(['middleware' => ['auth']], function()
    {
        Route::get('/dashboard',[
            'uses' => 'UserController@getDashboard',
            'as' => 'dashboard',
            'middleware' => 'auth'
        ]);
    });

    Route::resource('charities', 'charityController');

    Route::resource('results', 'charityController');

});

My charityController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\DB;
use App\charity;

class charityController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
     */
public function index()
{
    $charity = charity::all();
    return view('charities', ['charities' => $charity]);

    $search = \Request::get('search');
    $results = charities::where('name','like','%'.$search.'%')
                            ->orderBy('name')
                            ->paginate(20);

    return view('search',compact('results'));
}   

Relatively new to Laravel, however I have been through the documentation.

Thanks

0

There are 0 best solutions below