I'm trying to optimize the following query in Laravel using query builder and joins:
Product::select(DB::raw('
products.*
,(select name from users where users.id=products.user_id) as user_name
'))
->where(function ($query) use ($searchKey) {
if (trim($searchKey) != '') {
$query->where('name', 'like', '%' . trim($searchKey) . '%');
}
})
->orderBy($orderBy, $orderType)
->paginate(10);
Is there any further optimization or best practice that I can apply to this query?