daily pastebin goal
8%
SHARE
TWEET

Subscription/User query

a guest Dec 9th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Table users (model App\User)
  3.  id
  4.  email
  5.  active (0/1)
  6.  join_date
  7.  address etc
  8.  phone
  9.  
  10.  Table subscriptions (model App\Subscription)
  11.  id
  12.  user_id
  13.  box_id (what the person is subscribed to get)
  14.  amount
  15.  
  16. SELECT users.*, subscriptions.*
  17. FROM subscriptions
  18. JOIN users ON users.id = subscriptions.user_id
  19. WHERE users.active = 1
  20. */
  21. class Subscription extends Model
  22. {
  23.     public static function allActive()
  24.     {
  25.         // This works except it doesn't use the eloquent relationship
  26.         return static::where('users.active', 1)
  27.             ->join('users', 'users.id', '=', 'subscriptions.user_id')
  28.             ->select('users.*','subscriptions.*')
  29.             ->get();
  30.     }
  31.  
  32.     public function user()
  33.     {
  34.         return $this->belongsTo(User::class);
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top