poma

Experience

1,255

0 Best Reply Awards

  • Member Since 2 Years Ago
  • 6 Lessons Completed
  • 0 Favorites

23rd October, 2017

poma left a reply on How To Apply Different Rate Limits For Different Paths? • 7 months ago

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.

22nd October, 2017

poma left a reply on How To Apply Different Rate Limits For Different Paths? • 7 months ago

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? • 7 months ago

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');

17th October, 2017

poma left a reply on Poor WhereHas Performance • 7 months ago

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 • 7 months ago

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 • 7 months ago

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

poma left a reply on Poor WhereHas Performance • 7 months ago

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 • 7 months ago

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.

3rd October, 2017

poma left a reply on How To Enable Autocomplete For Laravel Resources In PhpStorm? • 7 months ago

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

2nd October, 2017

poma started a new conversation How To Enable Autocomplete For Laravel Resources In PhpStorm? • 7 months ago

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?

22nd September, 2017

poma started a new conversation How To Modify Relation When Converting To Json? • 8 months ago

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?

11th August, 2017

poma left a reply on How To Select Aggregates With Eloquent • 9 months ago

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 • 9 months ago

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.

1st September, 2015

poma left a reply on Throwing Exceptions Vs Returning Response • 2 years ago

@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.

16th August, 2015

poma left a reply on How To Declare Columns On Models? • 2 years ago

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

poma started a new conversation How To Declare Columns On Models? • 2 years ago

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?

15th August, 2015

poma left a reply on Is There A Package For Billing? • 2 years ago

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? • 2 years ago

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.

22nd July, 2015

poma left a reply on How To Do Basic Auth Before RouteServiceProvider? • 2 years ago

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? • 2 years ago

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? • 2 years ago

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.

17th July, 2015

poma left a reply on I Want My Cookie • 2 years ago

@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.

3rd July, 2015

poma left a reply on Throwing Exceptions Vs Returning Response • 2 years ago

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 • 2 years ago

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?

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.