Wednesday, March 15, 2017

How can I add condition on laravel eloquent? (Laravel 5.3)

My case is like this

For example, I have data like this

Data 1

id : 1

subject : chelsea jersey

buyer_id : 26

seller_id : 1

deleted_by_seller_at : 2017-03-15 16:42:59

Data 2

id : 2

subject : liverpool jersey

buyer_id : 8

seller_id : 1

deleted_by_buyer_at : 2017-03-15 16:42:59

I want to make like this :

If seller_id = auth()->user()->id

Message::whereNull('deleted_by_seller_at')          
       ->get();

If buyer_id = auth()->user()->id

Message::whereNull('deleted_by_buyer_at')
       ->get();

auth()->user()->id = user id being logged

seller_id and buyer_id taken from Data 1 and Data 2

How can I make it in one query?

It looks like it will be a condition in the query

If use if else, seemed unable. Because seller_id and buyer_id taken from the table. So it seems to use where. But I am confused implements it

Is there anyone who can help me?



via moses toh

Advertisement