I need to lookin' for data that the where clause have 3 condition, so its need OR operator twice, here is the code in my controller, I use laravel 8
$salesTransactions = SalesTransaction::when($request->keyword, function ($query) use ($request) {
$query
->where('tanggal_transaksi', 'like', "%{$request->keyword}%")
->orWhere('name', 'like', "%{$request->keyword}%")
->orWhere('status', 'like', "%{$request->keyword}%");
})->join('distributors', 'distributors.id', '=', 'sales_transactions.distributor_id')
->join('users', 'users.id', '=', 'distributors.user_id')
->select('sales_transactions.*', 'users.name')
->orderBy('tanggal_transaksi', 'DESC')->latest('sales_transactions.id')->paginate(25);
I got the error, but dont know how to solve, the problem is I cant use OR operator twice, any idea ? Thanks
If you need to group an "or" condition within parentheses, you may pass a closure as the first argument to the orWhere method. Like:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere(function($query) {
$query->where('name', 'Abigail')
->where('votes', '>', 50);
})
->get();
Related
I'm trying to do a simple search function. For that, I implemented the following query:
Profile::whereHas('accounts', function ($query) use ($id) {
$query->where('user_id', $id);
})
->where('username', 'LIKE', "%{$search}%")
->paginate(20);
The whereHas selects the profiles that the user with $id has.
Then, with the where we figure out if the search match with some username in the database.
Finally, we paginate the results.
All right so far.
The problem comes when I add a orWhere like that:
Profile::whereHas('accounts', function ($query) use ($id) {
$query->where('user_id', $id);
})
->where('username', 'LIKE', "%{$search}%")
->orWhere('fullname', 'LIKE', "%{$search}%")
->paginate(20);
I want that the query analyses if the search matchs with the username and/or the fullname. When I add the orWhere works, but the whereHas doesn't do its work and the results are from all the users.
You need to do it like this:
Profile::whereHas('accounts', function ($query) use ($id) {
$query->where('user_id', $id);
})
->where(function($query) use ($search) {
$query->where('username', 'LIKE', "%{$search}%")
->orWhere('fullname', 'LIKE', "%{$search}%");
})
->paginate(20);
I use slim 3 for microservice for that i install laravel eloquent and i write a laravel query like following
use Illuminate\Database\Capsule\Manager as DB;
DB::enableQueryLog();
$faqs = DB::table('rental_faq')
->where('id', '!=', '')
->where(function ($query) {
$query->where('isActive', '=', 1)
->orWhere('isDelete', '=', 'no');
})
->get();
then the problem is it shows a error like following
"
There was an error parsing JSON data
Unexpected token I in JSON at position 743
"
I want to perform and_where and or_where operation separated parenthesis
$maincat=25;
$subcat=30;
$faqs=DB::table('rental_faq')
->where('isActive',1)
->where('isDelete','no')
->where(function ($query) use($maincat,$subcat) {
$query->where('main_category',$maincat)
->whereRaw('FIND_IN_SET(?,sub_category)',[$subcat]);
})
->get();
Try with MySQL "NOT" function like so:
$faqs = DB::table('rental_faq')
->where('id', '<>', '')
->where(function ($query) {
$query->where('isActive', 1)
->orWhere('isDelete', 'no');
})
->get();
dd($faqs);
Let me know if it solves your issue.
I have custom query where ignored current row id:
$relatedVacancies = \App\Vacancy::whereHas('skills', function ($q) use ($vacancy) {
return $q->whereIn('skill_id', $vacancy->skills->pluck('skill_id'));
})->where('id', '!=', $vacancy->id)
->orWhere('employment', $vacancy->employment)->where('id', '!=', $vacancy->id)
->orWhere('title', 'like', '%'.$vacancy->title.'%')->where('id', '!=', $vacancy->id)
->get();
How I can use this condition one time only:
->where('id', '!=', $vacancy->id)
If you are trying to select based on three conditions:
->where([
('id', '!=', $vacancy->id),
('employment', '=', $vacancy->employment),
('title', 'like', '%'.$vacancy->title.'%'),
])->get();
Try this (based on https://laravel.com/docs/5.7/queries#parameter-grouping):
$relatedVacancies = \App\Vacancy::where('id', '!=', $vacancy->id)
->whereHas('skills', function ($q) use ($vacancy) {
return $q->whereIn('skill_id', $vacancy->skills->pluck('skill_id'));
})
->where(function ($query) {
$query->where('employment', $vacancy->employment)
->orWhere('title', 'like', '%'.$vacancy->title.'%');
})
->get();
Also check for this answer: https://stackoverflow.com/a/23009807/5458355
I want to run this query but with laravel query builder I'm not getting the exactly results using orWhere and where clause
SELECT * FROM lectures WHERE school=6 AND ( (period=3 AND class_section=3) OR (period=4 AND teacher=17) )
so how can I do it with laravel query builder can anyone help me out?
You need to use Parameter Grouping to translate your query to query builder form
DB::table('lectures)
->where('school', '=', 6)
->where(function ($query) {
$query->where(function ($query) {
$query->where('period', '=', 3)
->where('class_section', '=', 3)
})->orWhere(function ($query) {
$query->where('period', '=', 4)
->where('class_section', '=', 17)
});
})
->get();
I do request using query:
return Baber::where(function ($query) use ($request) {
// HERE CONDITION WHERE FOR USER TABLE
})->with("user")->orderBy('id', 'desc')->get();
How can I use where() inside query, that it will be comparabled with user table in with("user")?
I mean the following:
return Baber::where(function ($query) use ($request) {
$query->where('user.created_at', $request->date);
})->with("user")->orderBy('id', 'desc')->get();
It's known as constraining eager loads.
Baber::with(['user' => function ($query) use ($request) {
$query->where('user.created_at', '=', $request->date);
}])
->orderBy('id', 'desc')
->get();