php-laravelHow do I use the "WHERE LIKE" clause in PHP Laravel?
The WHERE LIKE clause is used in PHP Laravel to search for specific patterns in a database table. Here is an example of how it can be used:
$users = DB::table('users')
->where('name', 'LIKE', '%John%')
->get();
print_r($users);
The above code will return all users from the users table whose name contains "John". The output will be an array of user objects:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => John Doe
[email] => [email protected]
[age] => 30
)
[1] => stdClass Object
(
[id] => 2
[name] => John Smith
[email] => [email protected]
[age] => 25
)
)
Code explanation
DB::table('users')is used to select theuserstable from the database.->where('name', 'LIKE', '%John%')is used to specify the search pattern. In this case, it will search for names containing "John".->get()is used to execute the query and return the results.
Helpful links
More of Php Laravel
- ¿Cómo configurar PHP y Laravel desde cero?
- How do I set up a .gitlab-ci.yml file for a Laravel project using PHP?
- How do I configure Xdebug in the php.ini file for a Laravel project?
- How can I use the @yield directive in PHP Laravel?
- How can I create a website using the Laravel PHP framework and a template?
- How can I use PHP Laravel's ZipArchive library to create a zip file?
- How can I use Xdebug to debug a Laravel application written in PHP?
- How can I use the Laravel WhereIn method in PHP?
- How can I access an undefined array key in PHP Laravel?
- How do I run a seeder in Laravel using PHP?
See more codes...