0
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();
2
  • Show the code you have written to implement this query in Eloquent way. Commented Feb 8, 2019 at 13:37
  • @KamalPaliwal Thanks, I have updated the question Commented Feb 8, 2019 at 13:44

1 Answer 1

1
$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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.