0

Hey guys I am facing some problem I am using laravel 5.2 and I want to run a query like this:

SELECT * FROM Chat
WHERE (From='1234' AND Number='420') OR WHERE (From='420' AND Number='1234');

For that I am trying this but not getting the exact result:

    $products = Chat::where('from', '=', $from)->where('number', '=', $number)->orwhere('from', '=', $number)->orwhere('number', '=', $from)->orderBy('id', 'ASC')->paginate(10);

How can I make it work there is no such document where I can generate a query like that.

Nilay Singh
  • 2,201
  • 6
  • 31
  • 61
  • use \DB::select("SELECT * FROM Chat WHERE (From='1234' AND Number='420') OR WHERE (From='420' AND Number='1234')"); – Khan Shahrukh Oct 06 '16 at 19:37
  • This question has a good answer to what you're looking for: http://stackoverflow.com/questions/19325312/laravel-eloquent-multiple-where-clause-query – John Sparwasser Oct 06 '16 at 20:54

1 Answers1

3

You can try something like this

$products = Chat::where([
['from', '=', $from],
['number', '=', $number],
])->orwhere([
['from', '=', $number],
['number', '=', $from],
])->get();

with reference from here : How to create multiple where clause query using Laravel Eloquent?

Community
  • 1
  • 1
Saurabh Kiri
  • 79
  • 1
  • 6