How can I write this query with Laravel Eloquent
"SELECT * FROM links WHERE (user_id = 1 OR user_ip = '127.0.0.1') AND id = 53 LIMIT 1"
I have written this one: `
$link = Link::query()
->where('user_id', Auth::id())
->orWhere('user_ip', \request()->ip())
->with('clicks')
->findOrFail($id);
And the SQL version of this one is : "SELECT * FROM links WHERE user_id = 1 OR user_ip = '127.0.0.1' AND id = 53 LIMIT 1"
;
Its shows always unexpected result like : It shows the result of id 51 every time even when id is 52,53,54 but the result is shown for Id 51.
Thank you Tamim
You can group conditions when you pass a closure to where
.
$link = Link::query()
->where(function ($query) {
$query->where('user_id', Auth::id())
->orWhere('user_ip', \request()->ip())
})
->with('clicks')
->findOrFail($id);