You can use this example with versions of Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10. In this post, we'll show you how to use Laravel where like queries.
Example 1 : Laravel Where Like Query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function search(Request $request)
{
$keyword = 'example';
$posts = User::where('title', 'LIKE', "%$keyword%")->get();
dd($posts);
}
}
Example 2 : Laravel Where Like Query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function search(Request $request)
{
$keyword = 'example';
$posts = Post::where('title', 'LIKE', '%' . $keyword . '%')->get();
dd($posts);
}
}
Example 3 : Laravel Where Like Query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function search(Request $request)
{
$keyword = 'example';
$posts = Post::where('title', 'LIKE', '%' . $keyword . '%')
->orWhere('category', 'LIKE', '%' . $keyword . '%')
->get();
dd($posts);
}
}
Example 4 : Laravel Where Like Query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function search(Request $request)
{
$keyword = 'example';
$posts = Post::where('title', 'LIKE', "%$keyword%")
->orWhere('category', 'LIKE', "%$keyword%")
->get();
dd($posts);
}
}