poma

poma

Member Since 3 Years Ago

Experience Points 1,255
Experience
Level
Lessons Completed 6
Lessons
Completed
Best Reply Awards 0
Best Answer
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

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

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

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

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    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 Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

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

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

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

23 Oct
1 year ago

poma left a reply on How To Apply Different Rate Limits For Different Paths?

I think my solution in the past was to yank it out -- it's just a default Taylor wrote in so you don't have to. Then apply it in the api as needed.

That's what I wanted to avoid as mentioned in op. If I remove it then I will need to explicitly specify throttle for each route which makes code ugly.

22 Oct
1 year ago

poma left a reply on How To Apply Different Rate Limits For Different Paths?

The problem is that because global throttle is still attached and is above my middleware it will override x-ratelimit-remaining header with its value.

poma started a new conversation How To Apply Different Rate Limits For Different Paths?

I want to apply a global throttle to all my API requests but for some paths apply a special rule. I could remove throttle middleware from Kernel.php and split all routes in groups but this would create a mess in routes file. Is there a way to keep a throttle middleware in Kernel.php applied to all routes and override its behavior for certain routes like this

Route::get(...)->middleware('throttle:10,1');

OR

Route::get(...)->middleware('throttle-override:10,1');
17 Oct
1 year ago

poma left a reply on Poor WhereHas Performance

I did a test with join and execution takes 400ms, exactly like with whereIn.

My data set takes 25Gb in database and instance memory is only 1.5Gb RAM. This is an open source project running on donations so a price for appropriate production grade DB server greatly exceeds total budget.

poma left a reply on Poor WhereHas Performance

This is already mentioned above. replays table has 4M rows, players has 40M rows, all relevant columns are indexed, dataset doesn't fit into MySQL server memory.

Here is my view: https://github.com/poma/hotsapi/blob/master/app/Http/Controllers/ReplayController.php#L53

poma left a reply on Poor WhereHas Performance

The issue is that this query takes 70 seconds to execute on my dataset

poma left a reply on Poor WhereHas Performance

found that the correct query can be generated as:

Replay::whereIn('id', function ($query) {
    $query->select('replay_id')->from('players')->where('battletag_name', 'test');
})->limit(100);

Still have a question why exists performs so poorly and why it is the default behavior

poma started a new conversation Poor WhereHas Performance

I want to apply a where condition to relation. Here's what I do:

Replay::whereHas('players', function ($query) {
    $query->where('battletag_name', 'test');
})->limit(100);

It generates the following query:

select * from `replays` 
where exists (
    select * from `players` 
    where `replays`.`id` = `players`.`replay_id` 
      and `battletag_name` = 'test') 
order by `id` asc 
limit 100;

Which executes in 70 seconds. If I manually rewrite query like this:

select * from `replays` 
where id in (
    select replay_id from `players` 
    where `battletag_name` = 'test') 
order by `id` asc 
limit 100;

It executes in 0.4 seconds. Why where exists is the default behavior if it's so slow? Is there a way to generate the correct where in query with query builder or do I need to inject raw SQL? Maybe I'm doing something wrong altogether?

replays table has 4M rows, players has 40M rows, all relevant columns are indexed, dataset doesn't fit into MySQL server memory.

03 Oct
1 year ago

poma left a reply on How To Enable Autocomplete For Laravel Resources In PhpStorm?

Found the solution. Add @mixin <model> to Resource class phpdoc

02 Oct
1 year ago

poma started a new conversation How To Enable Autocomplete For Laravel Resources In PhpStorm?

Laravel 5.5 has a new API Resources feature, and it nicely redirects calls to model attributes (like $this->id). I use ide-helper:models to generate phpdocs for models that type-hints all model attributes. However, this does not apply to a resource and I get "Field accessed via magic method" squigglies. Is there a way to point it to model's phpdoc without copying it?

22 Sep
1 year ago

poma started a new conversation How To Modify Relation When Converting To Json?

I have a Parent that hasMany children:

class Parent extends Model
{
    public function children()
    {
        return $this->hasMany(Child::class);
    }
}


class Child extends Model
{
    public function parent()
    {
        return $this->belongsTo(Parent::class);
    }
}

Child model has only 2 attributes: parent_id and value. I want to "pluck" value when converting parent to json so that instead of

{
  "name": "name1",
  "children": [
    {
      "value": "value1"
    },
    {
      "value": "value2"
    }
  ]
}

I get

{
  "name": "name1",
  "children": [
    "value1",
    "value2"
  ]
}

I know that it can be accomplished by declaring a new getValuesAttribute and adding protected $appends = ['values']; to the Parent model but I want to do so without renaming the attribute (I want values to be in children array and not in values array). If I define mutator attribute with the same name as relation it obviously results in errors.

How can I do that?

11 Aug
1 year ago

poma left a reply on How To Select Aggregates With Eloquent

I already have that. I want to load aggregates (sum of amount) and not raw transactions.

poma started a new conversation How To Select Aggregates With Eloquent

I have 2 models: User and Transaction with user_id, type, and amount fields. I want to be able to select users with array of transactions grouped by type like so:

    { // User instance 
        'id': 123,
        'username': 'John'
        'transactions': [
            'type1': '231', // `type`: `sum(amount)`
            'type2': '543'
        ]
    }

I will select arrays of users so I need this to load eagerly. How should I write an Eloquent model for that? I've read all docs but still don't know how to approach this.

01 Sep
3 years ago

poma left a reply on Throwing Exceptions Vs Returning Response

@skliche can you explain why developers of laravel did it this way? I can't see any advantages of returning response directly. Throwing http exception will also result in "HTTP response with a 403 status code will automatically be returned and your controller method will not execute" but will also be consistent with the rest of code base which also throws exceptions in unexpected situations.

16 Aug
3 years ago

poma left a reply on How To Declare Columns On Models?

Got answer on SO php artisan ide-helper:models -W

poma started a new conversation How To Declare Columns On Models?

I want my Code Completion (PhpStorm) to work with Eloquent models but if I declare fields corresponding to DB columns they no longer work:

class Limit extends Model
{
    public $id;
    public $type;
    public $limit;
}

Is there some workaround?

15 Aug
3 years ago

poma left a reply on Is There A Package For Billing?

Working with money has higher quality requirements so I prefer a tested and reliable solution if one exists. If not I will write it myself of course.

poma started a new conversation Is There A Package For Billing?

I need ability to deposit money on users account, put it on hold when user spends them and withdraw it when order is fulfilled. Also I need a history of all transactions. Is there a library to do that? The only library I know of is Cashier but it is supposed to interact with subscriptions and this is not my case.

22 Jul
3 years ago

poma left a reply on How To Do Basic Auth Before RouteServiceProvider?

Create FormRequest for each resource method? I mean those methods are one-liners like

public function show(Article $article)
{
    return Response::json($article);
}

Creating separate for each request like this looks like overkill to me.

poma left a reply on How To Do Basic Auth Before RouteServiceProvider?

I don't have form requests for show resource method for example which just receives model instance

poma started a new conversation How To Do Basic Auth Before RouteServiceProvider?

I'm making a JSON API for my app. I've made stateless authorization middleware similar to basic auth. It looks for certain headers and executes Auth::onceUsingId($user->id); for each request. I'm also using Config::set('session.driver', 'array'); Config::set('cookie.driver', 'array');. The problem is that Auth::user() is not available in RouteServiceProvider which tries to check whether user owns certain Models because looks like it is executed before middleware. Is there a way to solve this? I believe I should modify Guard class somehow.

17 Jul
3 years ago

poma left a reply on I Want My Cookie

@pmall there are no other good solutions to tell the browser that download is started (important for generated files). Looks like I too have to use ugly double redirect workaround.

03 Jul
3 years ago

poma left a reply on Throwing Exceptions Vs Returning Response

I did. The problem is that errors in FormRequest validation don't throw exceptions that go through the handler. Instead they are wrapped in HttpResponseException that is rendered directly in illuminate/routing/route.php#136 and that is the problem I'm talking about.

poma started a new conversation Throwing Exceptions Vs Returning Response

I've recently tried to use FormRequest and found out that it returns 403 response instead of throwing HttpException when authorisation fails:

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Foundation/Http/FormRequest.php#L147

Is it a bug or intended behaviour? I'm building a REST API and need unified JSON format for all errors. I've already customised my exception handler for that. But how am I supposed to track down all random pieces of code that decide to return a response by themselves?