Search data via SQL using Laravel

Asked

Viewed 26 times

1

I need to select to return all users who have the initials of a name.

Example:

I type in my search input field: "paulo" returns to me all users who have this word.

I did some research and used it:

$usuarios = User::where('nome', 'LIKE', $search. '%')                
                ->paginate(); 
            return view('auth.adm.lista', compact( 'usuarios', 'title'));

This code returns me 15 users.

But if I go to PHPMYADMIN and type this sql:

SELECT * FROM `users` WHERE nome LIKE 'paulo%'

The above row returns 44 users.

Where can I be wrong that ends up limiting to 15 the search in Laravel ?

1 answer

2


In the paginate is the return on page (that is, page 1, page 2 and so according to the page have this result):

$usuarios = User::where('nome', 'LIKE', $search. '%')                
                ->paginate();  // isso retorna somente 15 registros

that by default the method paginate returns 15 records at a time, but this can be set to return another quantity, example: 10 records the code is as follows: ->paginate(10) consequently the pagination is 10 in 10 records.


If you want to return all records use the method get():

$usuarios = User::where('nome', 'LIKE', $search. '%')                
                ->get(); // retorna todas da pesquisa sem paginação.

Read the documentation: Database: Web site

Browser other questions tagged

You are not signed in. Login or sign up in order to post.