How to write SQL subquery using Laravel Eloquent? - laravel

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();

Related

Where clause wtih double OR operator in laravel 8 controller

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();

How to Filter Paid or Not Paid Student In laravel

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)
}
})

Laravel Eloquent making select on whereHas()

How can I select on whereHas() some columns in relation.
My current query looks like as it follows
Location::select(['title'])
->withCount(['job' => function($q) {
return $q->where('language_id', 1)->whereNull('deleted_at');
}])->whereHas('country', function ($q) {
return $q->where('id', '=', 80);
})->get();
and I tried
Location::select(['title', 'id as value'])->withCount(['job' => function($q) {
return $q->where('language_id', 1)->whereNull('deleted_at');
}])->whereHas('country', function ($q) {
return $q->select('title', 'iso3')->where('id', '=', 80);
})->get();
but nothing is getting returned on country relation. How do I refactor this query to get it work?
whereHas() doesn't eager load the relation, only filters results based on it. To eager load, use with().
Location::with('country:id,title,iso3')
->select(['title', 'id as value', 'country_id'])
->withCount(['job' => function($q) {
$q->where('language_id', 1)->whereNull('deleted_at');
}])
->whereHas('country', function ($q) {
$q->where('id', '=', 80);
})->get();

join query result in "Call to a member function leftJoin() on null"

I am trying to run a join query from my controller on laravel but i am getting "Call to a member function leftJoin() on null"
public function tracking_groupindividual(Request $request)
{
$user_id = $request->user_id;
$group_id = $request->group_id;
$get_group_individual_details = DB::table('group_usermapping as gu')
->select(
'um.user_id',
'um.user_name',
'um.image_path',
'um.mobile_number',
'ul.latitude',
'ul.longitude',
'um.isnearby_on',
'gu.isadmin',
'um.profile_status',
'ul.timedout'
)
->join('user_master as um','gu.user_id','=','um.user_id')
->join('groupusers_location as ul','ul.user_id','=','um.user_id','ul.group_id','=','gu.group_id')
->where(function ($query) use ($group_id) {
$query->where('gu.group_id', '=', $group_id)
->where('gu.isactive','=','1')->where('um.isactive','=','1');
})->get();
return $get_group_individual_details;
}
I found a problem in the second join and fixed it
public function tracking_groupindividual(Request $request)
{
$user_id = $request->user_id;
$group_id = $request->group_id;
$get_group_individual_details = DB::table('group_usermapping as gu')
->select(
'um.user_id',
'um.user_name',
'um.image_path',
'um.mobile_number',
'ul.latitude',
'ul.longitude',
'um.isnearby_on',
'gu.isadmin',
'um.profile_status',
'ul.timedout'
)
->join('user_master as um','gu.user_id','=','um.user_id')
->join('groupusers_location as ul', function ($join) {
$join->on('ul.user_id','=','um.user_id')
->where('ul.group_id','=','gu.group_id');
})
->where(function ($query) use ($group_id) {
$query->where('gu.group_id', '=', $group_id)
->where('gu.isactive','=','1')->where('um.isactive','=','1');
})->get();
return $get_group_individual_details;
}
try to use DB::raw() with aliases!!!
$get_group_individual_details = DB::table(DB::raw('group_usermapping as gu'))
->join(DB::raw('user_master as um'),DB::raw('gu.user_id'),'=',DB::raw('um.user_id'))
->join(DB::raw('groupusers_location as ul'),DB::raw('ul.user_id'),'=',DB::raw('um.user_id'),DB::raw('ul.group_id'),'=',DB::raw('gu.group_id'))
->select(DB::raw('um.user_id'),DB::raw('um.user_name'),DB::raw('um.image_path'),DB::raw('um.mobile_number'),DB::raw('ul.latitude'),DB::raw('ul.longitude'),DB::raw('um.isnearby_on'),DB::raw('gu.isadmin'),DB::raw('um.profile_status'),DB::raw('ul.timedout'))
->where(function ($query) use ($group_id) {
$query->where(DB::raw('gu.group_id'), '=', $group_id)->where(DB::raw('gu.isactive'),'=','1')->where(DB::raw('um.isactive'),'=','1');
})->get();
return $get_group_individual_details;
and replace use Illuminate\Support\Facades\DB; with use DB;

How to use where conditional in with Laravel?

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();

Resources