Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lawkunchi's avatar

Search Query

Help me with my Search Query, I get this error Call to undefined method App\User::search()


My Controller 

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class SearchController extends Controller
{

    >public function search(Request $request) {

        if ($request->has('search')) {
            $users = User::search($request->get('search'))->get();
        }

        else {
            $users = User::get();
        }

        return view('admin.clients', compact('users'));
    }
}


My Route
Route::get('admin/clients', [
    'uses' => 'UserController@getClients',
    'as' => 'admin.clients'
]);

<form  action="{{ route('clients.search') }}" method="GET" ><input type="text" name="search" ></form>
0 likes
10 replies
manelgavalda's avatar
Level 50

The error you have is because you are calling this.

User::search($request->get('search'))->get();

but the static method search doesn't exists in you user model, so you need to create it.

In your User Model:

public static function search($request) {
    // Your logic
}
1 like
lawkunchi's avatar
public static function search($request) {
        return 'admin.clients';
    }

I added this to my User Model and I still get this error

"Call to a member function get() on string"

manelgavalda's avatar

This error is because on your search method you are returning a string, so you can't use ->get() after the search method.

User::search($request->get('search'))->get();

This will work:

User::search($request->get('search'));

but your $users variable will be a string and you will have an error in your view, what are you trying to do in this search method?

lawkunchi's avatar

I have a table with users and I want to search certain users

I actually get this error now

Invalid argument supplied for foreach()

manelgavalda's avatar

You get this error because $users is a string('admin.clients').

In the case you have a "type" column on the user table to get the admin users you can do something like this so you don't need the search method in your users model.

// $request->search = 'admin'
$users = User::where('type', $request->search)->get();
lawkunchi's avatar

Column not found: 1054 Unknown column 'type' in 'where clause' (SQL: select * from users where type =jon)

i got this error now

manelgavalda's avatar

You need to create the field "type" if you want the query like mine.

Can you show us what your users table is like? So we can find the best solution.

lawkunchi's avatar
 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('phone_number');
            $table->string('email');
            $table->string('company_work');
            $table->string('password');
            $table->string('address');
            $table->string('state');
            $table->string('zip');
            $table->integer('active')->default(0);
            $table->rememberToken();
            $table->timestamps();
        });
    }
jaelyncyrus's avatar

Searchers making value-based questions don't need specifically need to discover more data about the item, they are prepared to purchase or play out an activity identifying with that item, administration or action. best essay writing service - australianessay.com. They simply need to discover the thing themselves.

Please or to participate in this conversation.