mercredi 10 février 2016

Getting Conditional Count in Join with Laravel Query Builder

I am trying to achieve the following with Laravel Query builder.

I have a table called deals . Below is the basic schema

id
deal_id
merchant_id
status
deal_text
timestamps

I also have another table called merchants whose schema is

id
merchant_id
merchant_name
about
timestamps

Currently I am getting deals using the following query

$deals = DB::table('deals')
        -> join ('merchants', 'deals.merchant_id', '=', 'merchants.merchant_id')
        -> where ('merchant_url_text', $merchant_url_text)
        -> get();

Since only 1 merchant is associated with a deal, I am getting deals and related merchant info with the query.

Now I have a 3rd table called tbl_deal_votes. Its schema looks like

id
deal_id
vote (1 if voted up, 0 if voted down)
timestamps

What I want to do is join this 3rd table (on deal_id) to my existing query and be able to also get the upvotes and down votes each deal has received.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire