0

I've been on this for a few hours without solution. I thought of laravel relationships but don't know how to pass a second condition because I need to relate with 3 tables. I'd like to use the query below in laravel.

SELECT
subscriptions.subscribed_to,
broadcasts. *,
FROM subscriptions
INNER JOIN broadcasts
WHERE subscriptions.subscriber = {$user_id}
AND (
    SELECT COUNT(*) FROM seen_broadcasts
    WHERE user_id = {$user_id}
    AND broadcast_id = broadcasts.id
) = 0
ORDER BY  broadcast.date DESC

There are 3 tables.

  1. subscriptions: subscriber_id subscribes to broadcaster_id.
  2. broadcasts: where broadcaster's message is saved.
  3. seen_broadcast: where the information of subscribers are saved when they read a broadcast. This helps us provide detailed stats to broadcaster. user_id = subscriber_user_id, broadcast_id = broadcast_message_id

I want to be able to get broadcasts from all broadcaster that userA has subscribed to and have not seen.

The query above currently works outside laravel.

4
  • CREATE and INSERT statements AND a desired result Commented Mar 18, 2016 at 7:35
  • There are 3 tables. 1. subscriptions: subscriber_id subscribes to broadcaster_id 2. broadcasts: where broadcaster's message is saved 3: seen_broadcast: where the information of subscribers are saved when they read a broadcast. This helps us provide detailed stats to broadcaster. user_id = subscriber_user_id, broadcast_id = broadcast_message_id I want to be able to get broadcasts from all broadcaster that userA has subscribed to and have not seen. I hope this is clear enough Commented Mar 18, 2016 at 8:46
  • I think you are missing the key fields that the inner join links on table1 inner join table2 on table1.field = table2.field Commented Mar 18, 2016 at 8:58
  • please post a class diagram or the related models Commented Mar 18, 2016 at 9:52

1 Answer 1

1

After much runarounds, I ended up with this:

$broadcast_result = DB::select( DB::raw("
                                        SELECT
                                        subscriptions.subscribed_to,
                                        broadcasts.*
                                        FROM subscriptions
                                        INNER JOIN broadcasts
                                        WHERE subscriptions.browser_agent_id = :subsc_id
                                        AND broadcasts.user_id = subscriptions.subscribed_to
                                        AND (
                                            SELECT COUNT(*) FROM broadcasts_seen
                                            WHERE broadcast_id = broadcasts.id
                                            AND subscriber_id = subscriptions.subscriber_id
                                        ) = 0
                                        ORDER BY  broadcasts.date DESC LIMIT 1
                                    "), array(
                                       'subsc_id' => $subscriber->id
                                     ));
$broadcast_set = $broadcast_result[0];

Also add use DB; in the controller.

If there's a better way to do this, please share.

Sign up to request clarification or add additional context in comments.

2 Comments

Without a proper schema and a desired, we have limited means for assessing this answer. If it works for you, then great, but you might as well keep that information to yourself too.
You can use relations but in Your case it will send many requests to db. So for Your case to do so it's ok. But I'll prefer to keep additional table to collect statistics and etc using jobs in laravel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.