0

In an attempt to program a search bar, I created a GET method and added a new controller where it gets the relevant data and returns it with the view.

    //This is the form in the view named "index.blade.php"

    {!! Form::open(['action' => 'SearchesController@search', 'method' => 'GET']) !!}   
        <form class="form-inline md-form mr-auto mb-4">

            {{Form::text('search', '', ['class'=>'form-control', 'placeholder'=>'Search Anything'])}}

            <button class="btn aqua-gradient btn-rounded btn-sm my-0" type="submit">Search</button>

       </form>
    {!! Form::close() !!}

    //---------------------------------------------------------

    //This is in the "web.php"

    Route::get('/posts/search', 'SearchesController@search');

    //---------------------------------------------------------

    //The following code is the controller named "SearchesController.php"

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    use Illuminate\Support\Facades\Storage;
    use App\Post;
    use DB;

    use Illuminate\Support\Facades\Auth;

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

            $search = metaphone($request->input('search'));


            $posts = Post::where('sounds_like','LIKE',"%{$search}%")
                ->orderBy('title', 'desc')
                ->paginate(10)
                ->get();

            return view('posts.index')->with('posts',$posts);
        }
    }

I expected the output of a view with all the data returned with the view, but instead got the error message :

"Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in /Applications/AMPPS/www/lsapp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 23 and at least 1 expected"

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
Philip
  • 3
  • 1
  • 2

1 Answers1

2

You desn't need a get() if you are using paginate(). Paginate will execute the query too and will create a collection, and the get() method will be executed as an instance of this get() and will require the key parameter . So you can remove it:

public function search(Request $request)
{
    // ...


    $posts = Post::where('sounds_like','LIKE','%'. $search . '%')
        ->orderBy('title', 'desc')
        ->paginate(10);

    return view('posts.index')->with('posts',$posts);
}

Not related to the error, but also you have a form inside a form. Choose one and remove the other:

{!! Form::open(['action' => 'SearchesController@search', 'method' => 'GET', 'class'=>'form-inline md-form mr-auto mb-4']) !!} 

        {{Form::text('search', '', ['class'=>'form-control', 'placeholder'=>'Search Anything'])}}

        <button class="btn aqua-gradient btn-rounded btn-sm my-0" type="submit">Search</button>

{!! Form::close() !!}
porloscerros Ψ
  • 4,808
  • 2
  • 11
  • 20