I have a query that is not returning all the results it should.
I'm using Laravel, but the result is the same if I write the query directly in PHPMyAdmin:
clientpages::selectRaw('page as value, COUNT(ID) as `count`')
->where('page', 'like', '%blog%')
->orWhere('page', 'like', '%home%')
->whereBetween('date', [$range['from'], $range['to']]);
The problem is that the query only processes the first like. It only returns the pages that have blog in them and not the ones with home.
The culprit is the date condition. If I remove that, it works as expected, but what is the right way to use these three conditions all at once?
Like you would wrap a normal SQL query in parentheses, you can do the same in Laravel like this:
clientpages::selectRaw('page as value, COUNT(ID) as `count`')
->where(function($query) {
$query->where('page', 'like', '%blog%')
->orWhere('page', 'like', '%home%')
})
->whereBetween('date', [$range['from'], $range['to']]);
$clientpages = DB::table('clientpages')
->where('page', 'like', '%blog%')
->orWhere('page', 'like', '%home%')
->whereBetween('date', [$range['from'], $range['to']])
->get(); //or count() if you just want the count
clientpages::where('page', 'like', '%blog%')
->orWhere('page', 'like', '%home%')
->whereBetween('date', [$range['from'], $range['to']])
->get(); //or count() if you just want the count
Related
I have a query that works but I need to get it into the Query Builder:
RIGHT JOIN (tlkpSUDesc RIGHT JOIN tblSU ON tlkpSUDesc.SUDescCode = tblSU.SUDescCode) ON tlkpSUTyp.SUTypCode = tblSU.SUTypCode
I can figure out how to do it. I tired this:
->rightJoin('tlkpSUDesc', function($join) {
$join->on('tblSU', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode');
}
'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode')
But that didn't work. I also tried
->rightJoin('tlkpSUDesc', 'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode')
->rightJoin('tblSU', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode')
But that didn't work either.
Thanks!
I agree with #matiaslauriti that there's nothing wrong with reaching for raw expressions. But, since you asked and it can be done with the query builder, try this:
$query = \DB::query();
$query->rightJoin('tlkpSUDesc', function ($query) {
$query->rightJoin('tblSu', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode')->on(
'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode');
})->toSql();
please someone can optimize this query according to Laravel query builder with some help of 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)
Maybe something like this:
//Start your query as usual, but just join the user data in. Do not use paginate function yet as this will trigger the query to execute.
$productQuery = Product
::selectRaw('products.*, users.name as user_name')
->join('users', 'users.id', 'products.user_id')
->orderBy($orderBy, $orderType);
//Check (for each param) if you want to add where clause
//You dont need to nest the where function, unless you have more filters, and need to group statements together
if(!empty(trim($searchKey))) {
$productQuery->where('name', 'like', '%' . trim($searchKey) . '%');
}
//Execute query
$products = $productQuery->paginate(10);
Note that the query builder only touches the db with specific functions like chunk, get, first or paginate(there are more). When building the query, you have full freedom of adding filters/ordering/grouping untill you execute the query.
I hope it helps, please let me know if it worked for you.
This is the code that is already working in mysql database but I want to convert it into Laravel 5.6
SELECT *
FROM `listings`
WHERE (
country_id=1
AND (state=32 or city=8)
AND (listing_id LIKE "%6562%" OR title LIKE "%6562%"))
Supposedly you have a model called Listing which takes care of the listings table. You can write the query like this:
$listings = App\Listing::where('field1', 1)
->where(function ($query) {
$query->where('field2', 32);
$query->orWhere('field3', 8);
})
->where(function ($query) {
$query->where('field4', 'LIKE', '%6562%');
$query->orWhere('field5', 'LIKE', '%6562%');
})
->get();
The first parameter of the where method can be a callback which can achieve this type of grouping (field2=32 or field3=8)
I am using like operator two times in different where clauses. If I try one where clause, It works fine, but with both. It doesn't work.
Here is my code.
$products = Product::where(['is_live'=>'N'])->where(['is_active'=>'Y'])->where(['is_deleted'=>'N']);
$products->where('payment_mode', 'like', '%Credit/Debit Card%');
$products->where('payment_mode', 'like', '%Cash%');
$products = $products->get();
Please help.
You must be use orWhere
$products = Product::where(['is_live'=>'N'])
->where(['is_active'=>'Y'])
->where(['is_deleted'=>'N'])
->where(function($query) {
$query->where('payment_mode', 'like', '%Credit/Debit Card%')
->orwhere('payment_mode', 'like', '%Cash%');
});
or you can use this
Product::where([
'is_live'=>'N',
'is_active'=>'Y',
'is_deleted'=>'N'
])->where(function ($query) {
$query
->where('payment_mode', 'like', '%Credit/Debit Card%')
->orwhere('payment_mode', 'like', '%Cash%');
})->get();
I'm trying to complete a search form in Laravel 5.2. I've run into a problem where the $search will be a question mark in my sql query. I've used ->toSql() to get the result of the query. I'm using DB::table() with a WHERE like this:
->where('clientname', 'LIKE', $search)
->orWhere('ClientFirstName', 'LIKE', [$search])
->orWhere('ClientLastName', 'LIKE', DB::raw($search))
Each of these create in different results in the query:
"where clientname LIKE ? or ClientFirstName LIKE ? or ClientLastName LIKE test "
Using the DB::raw() I get the search parameter from the form, but I can't make it a wildcard search with %, and I can't put single or double quotes around it to have the proper syntax to complete the search query.
Any advice is appreciated!
Try using this code
->where('clientname', 'LIKE', '%'.$search.'%')
->orWhere('ClientFirstName', 'LIKE', '%'.$search.'%')
->orWhere('ClientLastName', 'LIKE', '%'.$search.'%')->get();
->where(function ($q) use ($searchValues) {
foreach ($searchValues as $value) {
$q->orWhere('ClientFirstName', 'LIKE', "%{$value}%");
}
})
->orWhere(function ($q) use ($searchValues) {
foreach ($searchValues as $value) {
$q->orWhere('ClientLastName', 'LIKE', "%{$value}%");
}
})
I ended up going with this as my final working solution.