I'm trying to change the following query to laravel :
SELECT
*
FROM
usrusrs uu
,posts p
WHERE
(
uu.user_id = $u_id
or
uu.friend_id = $u_id
)
and
(
uu.user_id = p.user_id
OR
uu.friend_id = p.user_id
)
I tried the following, but it's giving different output. I think there might be something wrong with the joining between the two tables. any suggestions ?
$usrusrmembs = DB::table('usrusrs')
->join('posts', 'posts.user_id', '=', 'usrusrs.user_id')
->orwhere('posts.user_id', '=', 'usrusrs.friend_id')
->where('usrusrs.user_id', $u_id)
->orwhere('usrusrs.friend_id', $u_id)
->get();
(x OR y)in your query, you need to do a->where(function($query) { $query->where(x); $query->orWhere(y); })to enclose the set in parentheses.Usermodel would have afunction posts() { return $this->hasMany(Post::class); }, which allows you to do things like$user->postsandUser::whereHas('posts'). (That's the basics; far more complicated setups/queries are 100% possible.) laravel.com/docs/5.8/eloquent-relationships