Related
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();
I have just created some payment system for a school and I need to filter my data to Paid or Not Paid category.
$queryBuilder = Student::query()
->leftJoin('fee_scheem_maps', 'students.students_id', '=', 'fee_scheem_maps.sfc_student_id')
->leftJoin('fee_scheems', 'fee_scheem_maps.sfc_feescheem_id', '=', 'fee_scheems.fee_scheems_id')
->leftJoin('individual_fee_scheems', 'fee_scheem_maps.sifc_feescheem_id', '=', 'individual_fee_scheems.ifs_id')
->leftJoin('fee_groups', 'fee_scheems.fee_scheems_id', '=', 'fee_groups.fg_fee_scheem_id')
->leftJoin('school_fee_collectors', 'students.students_id', '=', 'school_fee_collectors.fc_student_id')
// ->Where('school_fee_collectors.fc_fee_group_id', $fromDate)
// ->orWhereNull('school_fee_collectors.fc_fee_group_id', $pstatus)
// ->orWhereNull('school_fee_collectors.fc_fee_group_id', $fromDate)
->when($fromDate, function ($query) use ($fromDate) {
return $query->where('school_fee_collectors.fc_fee_group_id', $fromDate);
})
->select('*', DB::raw('count(students_id) as total'));
It is working fine with one clause: I need to randomly choose where or notWhere in my case...
$queryBuilder = Student::query()
->leftJoin('fee_scheem_maps', 'students.students_id', '=', 'fee_scheem_maps.sfc_student_id')
->leftJoin('fee_scheems', 'fee_scheem_maps.sfc_feescheem_id', '=', 'fee_scheems.fee_scheems_id')
->leftJoin('individual_fee_scheems', 'fee_scheem_maps.sifc_feescheem_id', '=', 'individual_fee_scheems.ifs_id')
->leftJoin('fee_groups', 'fee_scheems.fee_scheems_id', '=', 'fee_groups.fg_fee_scheem_id')
->leftJoin('school_fee_collectors', 'students.students_id', '=', 'school_fee_collectors.fc_student_id')
->when($fromDate, function ($query) use ($fromDate) {
return $query->where('school_fee_collectors.fc_fee_group_id', $fromDate);
})
->where(function($query) use ($request){
if($request->paid_status){
$query->where('table_name.columName',$request->paid_status)
}
})->select('*', DB::raw('count(students_id) as total'));
you can use where like this , when your $request->paid_status is null means have no filter about paid not paid thats time query is return everything but when is have filter thats time only this where is working
->where(function($query) use ($request){
if($request->paid_status){
$query->where('table_name.columName',$request->paid_status)
}
})
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.
select properties.address, properties.unit_type, rents.date, rents.amount, lease_requests.security_deposit
from properties
left join rents on rents.property_id = properties.id
left join lease_requests on lease_requests.property_id = properties.id
where (rents.status = 'paid' and rents.date between '2019-01-07' and '2019-02-07')
or (lease_requests.security_deposit_status = 'paid' and lease_requests.move_in between '2019-01-07' and '2019-02-07')
How can I rewrite this sql query in Laravel, but not by using raw sql, but with combining ->join, ->where, ->orWhere? I know that I should use function within one of these, but I am not sure within which one would that be. Does all of those functions accept function as second parameter?
EDIT
Here is the code which I have written in order to accomplish this, but it's not retrieving exactly the same data as the sql code I have written up there, because I am not sure how to implement query with brackets using Laravel:
Property::join('rents', 'properties.id', '=', 'rents.property_id')
->leftJoin('lease_requests', 'properties.id', '=', 'lease_requests.property_id')
->where('properties.address', $property['address'])
->whereBetween('move_in', [$from,$to])
->whereBetween('date', [$from,$to])
->where('rents.status', 'paid')
->orWhere('security_deposit_status', 'paid')
->get(['properties.address', 'properties.unit_type', 'rents.date', 'rents.amount', 'lease_requests.move_in', 'lease_requests.security_deposit'])->toArray();
$status = 'paid'; $dateStart = '2019-01-07'; $dateEnd = '2019-02-07';
DB::table('properties')
->selectRaw('properties.address, properties.unit_type, rents.date, rents.amount, lease_requests.security_deposit')
->leftJoin('rents', 'rents.property_id', '=', 'properties.id')
->leftJoin('lease_requests', 'lease_requests.property_id', '=', 'properties.id')
->where(function ($query) use ($status, $dateStart, $dateEnd) {
$query->where('rents.status', $status)->whereBetween('rents.date', [$dateStart, $dateEnd]);
})
->orWhere(function ($query) use ($status, $dateStart, $dateEnd) {
$query->where('lease_requests.security_deposit_status', $status)->whereBetween('lease_requests.move_in', [$dateStart, $dateEnd]);
})->get();
you must provide a function to get the parenthesis ;)
EDIT with your code :
Property::leftJoin('rents', 'properties.id', '=', 'rents.property_id')
->leftJoin('lease_requests', 'properties.id', '=', 'lease_requests.property_id')
->where(function ($query) use ($status, $dateStart, $dateEnd) {
$query
->where('rents.status', $status)
->whereBetween('rents.date', [$dateStart, $dateEnd]);
})
->orWhere(function ($query) use ($status, $dateStart, $dateEnd) {
$query
->where('lease_requests.security_deposit_status', $status)
->whereBetween('lease_requests.move_in', [$dateStart, $dateEnd]);
})
->get([
'properties.address', 'properties.unit_type',
'rents.date', 'rents.amount', 'lease_requests.move_in',
'lease_requests.security_deposit'
])->toArray();
I'm trying to add a condition using a JOIN query with Laravel Query Builder.
<?php
$results = DB::select('
SELECT DISTINCT
*
FROM
rooms
LEFT JOIN bookings
ON rooms.id = bookings.room_type_id
AND ( bookings.arrival between ? and ?
OR bookings.departure between ? and ? )
WHERE
bookings.room_type_id IS NULL
LIMIT 20',
array('2012-05-01', '2012-05-10', '2012-05-01', '2012-05-10')
);
I know I can use Raw Expressions but then there will be SQL injection points. I've tried the following with Query Builder but the generated query (and obviously, query results) aren't what I intended:
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function ($join) {
$join->on('rooms.id', '=', 'bookings.room_type_id');
})
->whereBetween('arrival', array('2012-05-01', '2012-05-10'))
->whereBetween('departure', array('2012-05-01', '2012-05-10'))
->where('bookings.room_type_id', '=', null)
->get();
This is the generated query by Laravel:
select distinct * from `room_type_info`
left join `bookings`
on `room_type_info`.`id` = `bookings`.`room_type_id`
where `arrival` between ? and ?
and `departure` between ? and ?
and `bookings`.`room_type_id` is null
As you can see, the query output doesn't have the structure (especially under JOIN scope). Is it possible to add additional conditions under the JOIN?
How can I build the same query using Laravel's Query Builder (if possible) Is it better to use Eloquent, or should stay with DB::select?
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function($join)
{
$join->on('rooms.id', '=', 'bookings.room_type_id');
$join->on('arrival','>=',DB::raw("'2012-05-01'"));
$join->on('arrival','<=',DB::raw("'2012-05-10'"));
$join->on('departure','>=',DB::raw("'2012-05-01'"));
$join->on('departure','<=',DB::raw("'2012-05-10'"));
})
->where('bookings.room_type_id', '=', NULL)
->get();
Not quite sure if the between clause can be added to the join in laravel.
Notes:
DB::raw() instructs Laravel not to put back quotes.
By passing a closure to join methods you can add more join conditions to it, on() will add AND condition and orOn() will add OR condition.
If you have some params, you can do this.
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function($join) use ($param1, $param2)
{
$join->on('rooms.id', '=', 'bookings.room_type_id');
$join->on('arrival','=',DB::raw("'".$param1."'"));
$join->on('arrival','=',DB::raw("'".$param2."'"));
})
->where('bookings.room_type_id', '=', NULL)
->get();
and then return your query
return $results;
You can replicate those brackets in the left join:
LEFT JOIN bookings
ON rooms.id = bookings.room_type_id
AND ( bookings.arrival between ? and ?
OR bookings.departure between ? and ? )
is
->leftJoin('bookings', function($join){
$join->on('rooms.id', '=', 'bookings.room_type_id');
$join->on(DB::raw('( bookings.arrival between ? and ? OR bookings.departure between ? and ? )'), DB::raw(''), DB::raw(''));
})
You'll then have to set the bindings later using "setBindings" as described in this SO post:
How to bind parameters to a raw DB query in Laravel that's used on a model?
It's not pretty but it works.
The sql query sample like this
LEFT JOIN bookings
ON rooms.id = bookings.room_type_id
AND (bookings.arrival = ?
OR bookings.departure = ?)
Laravel join with multiple conditions
->leftJoin('bookings', function($join) use ($param1, $param2) {
$join->on('rooms.id', '=', 'bookings.room_type_id');
$join->on(function($query) use ($param1, $param2) {
$query->on('bookings.arrival', '=', $param1);
$query->orOn('departure', '=',$param2);
});
})
I am using laravel5.2 and we can add joins with different options, you can modify as per your requirement.
Option 1:
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);//you add more joins here
})// and you add more joins here
->get();
Option 2:
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')// you may add more joins
->select('users.*', 'contacts.phone', 'orders.price')
->get();
option 3:
$users = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->leftJoin('...', '...', '...', '...')// you may add more joins
->get();
For conditional params we can use where,
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function($join) use ($param)
{
$join->on('rooms.id', '=', 'bookings.room_type_id')
->where('arrival','=', $param);
})
->where('bookings.room_type_id', '=', NULL)
->get();
There's a difference between the raw queries and standard selects (between the DB::raw and DB::select methods).
You can do what you want using a DB::select and simply dropping in the ? placeholder much like you do with prepared statements (it's actually what it's doing).
A small example:
$results = DB::select('SELECT * FROM user WHERE username=?', ['jason']);
The second parameter is an array of values that will be used to replace the placeholders in the query from left to right.
My five cents for scheme LEFT JOIN ON (.. or ..) and (.. or ..) and ..
->join('checks','checks.id','check_id')
->leftJoin('schema_risks', function (JoinClause $join) use($order_type_id, $check_group_id, $filial_id){
$join->on(function($join){
$join->on('schema_risks.check_method_id','=', 'check_id')
->orWhereNull('schema_risks.check_method_id')
;
})
->on(function($join) use ($order_type_id) {
$join->where('schema_risks.order_type_id', $order_type_id)
->orWhereNull('schema_risks.order_type_id')
;
})
->on(function($join) use ($check_group_id) {
$join->where('schema_risks.check_group_id', $check_group_id)
->orWhereNull('schema_risks.check_group_id')
;
})
->on(function($join) use($filial_id){
$join->whereNull('schema_risks.filial_id');
if ($filial_id){
$join->orWhere('schema_risks.filial_id', $filial_id);
}
})
->on(function($join){
$join->whereNull('schema_risks.check_risk_level_id')
->orWhere('schema_risks.check_risk_level_id', '>' , CheckRiskLevel::CRL_NORMALLLY );
})
;
})