Yorkata's avatar

Undefined method in web.php

I want to add a search bar to my application. Here's the logic I am trying to implement in my code: (My first time doing a search bar)

Route::any ( '/search', function () {
    $q = Input::get('q');
    if($q != ""){
    $competition = Competition::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'email', 'LIKE', '%' . $q . '%' )->paginate (5)->setPath ( '' );
    $pagination = $competition->appends ( array (
       'q' => Input::get( 'q' ) 
     ) );
    if (count ( $competition ) > 0)
     return view ( 'welcome' )->withDetails ( $competition )->withQuery ( $q );
    }
     return view ( 'welcome' )->withMessage ( 'No Details found. Try to search again !' );
   } );

The ::get, ->withDetails, ->withMessage are returning the following errosr:

Undefined method 'get'.intelephense(1013)
Undefined method 'withDetails'.intelephense(1013)
Undefined method 'withMessage'.intelephense(1013)

Any idea why I cant use them? What should I add? Any and all help gratefully received.

0 likes
3 replies
Sinnbeck's avatar

Is that some very old tutorial you are following?

use Illuminate\Http\Request;


Route::any ( '/search', function (Request $request) {
    $q = $request->get('q');
    if($q != ""){
    $competition = Competition::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'email', 'LIKE', '%' . $q . '%' )->paginate (5)->setPath ( '' );
    $pagination = $competition->appends ( array (
       'q' => $request->get( 'q' ) 
     ) );
    if (count ( $competition ) > 0)
     return view ( 'welcome' )->withDetails ( $competition )->withQuery ( $q );
    }
     return view ( 'welcome' )->withMessage ( 'No Details found. Try to search again !' );
   } );

I also highly recommend using a controller instead of putting the whole code in web.php

1 like
Yorkata's avatar

@Sinnbeck The article was made around 4 years ago. If that is not an optimal way can you suggest me something else I should follow? Also withDetails and withMessage are still giving me the same problem.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Yorkata I recommend as always watching https://laracasts.com/series/laravel-8-from-scratch

Use my method for getting the request object. And those are just IDE errors. Should still work. But you should be able to do this instead to not get the ide error

     return view ( 'welcome' )->with('details', $competition )->withQuery ( $q );
1 like

Please or to participate in this conversation.