TomNeal

TomNeal

Member Since 1 Year Ago

Experience Points
6,330
Total
Experience

3,670 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
62
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your 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-in-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 Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist 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.

Level 2
6,330 XP
Aug
04
1 month ago
Activity icon

Replied to Quickly Counting Records With Eloquent

@snapey The related model loading was because it is used in the export and I co-opted this method for the count. Many thanks for your feedback, every day is a school day :-)

After thinking about it we will only show count figures if it is not really high anyway as if its beyond 1 million, the count is not significant. We just wanted to give the user an idea of what to expect before queuing up the csv export, and because we reject export requests that are too large.

Many thanks from a long time lurker!

Activity icon

Replied to Quickly Counting Records With Eloquent

Ahh OK, I misunderstood as in the export I just return the result of queryWithParameters (using the Laravel Excel package with FromQuery). Thank you, I will check the table :)

Activity icon

Replied to Quickly Counting Records With Eloquent

Thanks, I thought it must be something like that, as I said this query is used for the export itself so sort of borrowed it for the count. We are not in production yet though. Would you be able to show me what that query would look like? :)

Activity icon

Started a new Conversation Quickly Counting Records With Eloquent

Hi,

I display in my application a count of the records that exist within a table. When the user changes an input i.e. selects a different start date, it sends an ajax request to the controller below to get an updated estimated count based on the user's selection. The idea is that they can see how many records exist based on their selected parameters.

I filter the query using three user selected options: date_start, date_end and sensor_id. This method filters the result:

public static function queryWithParameters($parameters) {
        $sensor_data = SensorData::query();
        $sensor_data = $sensor_data->with(['sensor'])
           ->whereBetween('created_at', [$parameters['date_start'], $parameters['date_end']]);
        if((int)$parameters['sensor_id'] !== 0) {
            $sensor_data = $sensor_data->where('sensor_id', $parameters['sensor_id']);
        }
        return $sensor_data;
 }

I use this method elsewhere to return data for a csv export, however, for this I use in a controller and return the count of the result:

public function index() {
        $sensor_data = SensorData::queryWithParameters(request());
        return response()->json($sensor_data->count());
 }

This all works as intended, however, this table is expected to eventually contain millions of rows. WIth 3.2m rows, it takes about 2 seconds to update the count. Is there a better way of writing this so that the query returns a quicker count? We are only really after an estimate of what is there!

Hope that makes sense - thank you for your help!