WelshyRob

WelshyRob

Member Since 2 Months Ago

Experience Points 260
Experience Level 1

4,740 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 2
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

16 May
2 months ago

WelshyRob left a reply on Count Distinct Children Within Relationship

@kevinbui @tisuchi - Thanks for your direction.

I ended up writing this, which does what I need.

Can you see a way to refactor it at all, it seems a bit clunky!

''' $sessions = Session::selectRaw('events.key AS action, COUNT(events.key) AS count') ->join('events', 'sessions.id', '=', 'events.session_id') ->when(request('start_date') && request('end_date'), function ($q) { $q->whereBetween(request('start_date'), request('end_date')); })->when(request('platform'), function($q){ $q->Platform(request('platform')); })->when(request('device_id'), function($q) { $q->Device(request('device_id')); })->when(request('version'), function($q){ $q->Version(request('device_id')); })->groupBy('events.key')->get(); '''

Ideally I'd like to combine all of the scopes into a single query string / object and append that to:

''' Session::selectRaw()->myCombinedScopes()->get() '''

13 May
2 months ago

WelshyRob started a new conversation Count Distinct Children With Parent Filtered

Hi, I'm trying to build a basic tracking system. I've setup a system using related tables and I'd like to avoid using raw DB queries if possible.

Here's how my models relate: Each Event belongs to a Session and a Session has multiple events.

class event extends Model
{
    //
    public function session()
    {
        return $this->belongsTo('App\session');
    }
}
class session extends Model
{
    public function events()
    {
        return $this->hasMany('App\event','session_id', 'id');
    }

    // .... Scopes......
    }```

 
So the session table has the date range and other filters, like version and platform and is filtered by the use of scopes.

Each Event has a Key, which is the name of the event that occurred and essentially I just want to count how many distinct keys are found but filtered by the session. 

If I was to write this using a raw query it would be along the lines of 

SELECT events.key AS action, events.session_id FROM events INNER JOIN sessions ON sessions.id = events.session_id where sessions.end_time > '2018-05-19 04:42:00' and sessions.end_time < '2019-05-19 04:42:00'


I've read the docs on Laravel relationships and I just can't see how I can generate the same kind of query using 
eloquent. I've tried using 'with', but it seems to either ignore the group by or cause errors. 


public function get_events_by_filter(Request $request) {
    $query = session::query();
    $query->when(request('platform'), function ($q) {
        return $q->Platform(request('platform'));
    });
   
    $events = $query->with(['events' => function ($query) {
        $query->groupBy('id','key')->count();
    }])->get();

}

Final output should be along the lines of: 

{ 
    "KEY_1": 19,
    "KEY_2": 11,
    ....
    "KEY_99" : 2
}


Thank you for your time