After spending hours on this, finally I felt to post this question here. I am trying to join tables. Due to some circumstances, I have to use the query in the same way that I have pasted below.
select posts.id,
last_user_comments.created_at
from posts
left join comments as last_user_comments
on (select comments.id
from comments
join users on users.id = comments.user_id
join posts on posts.user_id = users.id
where commnets.created_at between '2023-01-01' and '2023-01-31'
order by comments.created_at desc
limit 1) = comments.id;
The table structures are quite simple.
users
-> id
, name
posts
-> id
, title
, user_id
, created_at
, updated_at
comments
-> id
, comment
, post_id
, user_id
, created_at
, updated_at
FYI, I don’t want to manipulate the raw query. I want to convert it into Laravel Eloquent. Since I know, Laravel uses whereColumn behind the JoinClause::on() method. so, I just want to figure out how can we write an eloquent for this.
Looking forward to your answers, Artisans.
2
Answers
I could make it happen with the below query:
I think this example can give you a brief idea,
Create a query for your subquery,
And use it in main query