falnyr

Backend/DevOps engineer at Auckland

Experience

9,020

0 Best Reply Awards

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

14th February, 2018

falnyr started a new conversation Lazy Loading Attributes • 4 months ago

Hi, I have an attribute method getFooAttribute which performs quite a complicated algorithm.

The models resource object contains this attribute but I'd like to have a condition for it (only for show method, not index)

With relationships I am using whenLoaded, is there something similar I can use for this case?

20th December, 2017

falnyr left a reply on Enforcing Strict Types In Php 7 • 6 months ago

Wrong, wrong, wrong.

Declaration of strict_types needs to be present in every file that wants to be managed as strict. If you would be able to set that globally, there is no certainty that third-party libraries that you use are prepared for strict types.

28th November, 2017

falnyr left a reply on Updating Model With Relationships In A Single Request • 6 months ago

Could you please reference the whole method? Even setting only the properties that were passed in the form.

27th November, 2017

falnyr started a new conversation Updating Model With Relationships In A Single Request • 6 months ago

Hey there, I have a Post and Tag entities. Post can have many Tags and I want to update them in a single method like:

post.name = 'Foo' post.random = 'Alpha' tag[1][id] = 1 tag[1][name] = 'Bar' tag[2][id] = 2 tag[2][name] = 'Baz'

public function update(Request $request, Post $post)
{
    $post->update([
        'name' => $request->get('name'),
        'excerpt' => $request->get('excerpt'),
    ]);

    if ($tags = $request->get('tags')) {
        foreach ($tags as $tag) {
            $tagObject = Tag::where('id', $tag['id'])
                ->where('post_id', $post->id)
                ->firstOrFail();
            $tagObject->update($tag);
        }
    }

    return $post;
}

Now this ofc. removes the excerpt (since no value was provided) and saving the tags feels a little weird - there must be a better way.

Do you know how to save only fields that were provided in request, discard the non-existing (random) and save relationships efficiently?

15th November, 2017

falnyr started a new conversation Validation In API • 7 months ago

Hey there, I am trying to use validation method in my controller:

$validation = $this->validate($request, ['title' => 'required']);

Instead of getting errors or exception I am redirected to /, since Exception handler is providing validation errors to be displayed in HTML. (For this case I also registered my own exception handler, but it doesn't feel right)

Is there something like Laravel "API mode" which adds validation errors to the response and manages exceptions or do I need to take care of that part?

14th November, 2017

falnyr started a new conversation Testing API With Bearer Token • 7 months ago

Hey, I am trying to test several endpoints in my API. Authentication is provided via laravel/passport's Password Grant Token.

What I have right now is a TestCase class with this:

public function setUp()
    {
        parent::setUp();

        $clientRepository = new ClientRepository();
        $this->client = $clientRepository->createPasswordGrantClient(
            null, 'Test Personal Access Client', ''
        );

        $this->user = create(User::class, [
            'password' => bcrypt('secret')
        ]);

        $response = parent::postJson('/oauth/token', [
            'client_secret' => $this->client->secret,
            'client_id' => $this->client->id,
            'grant_type' => 'password',
            'username' => $this->user->email,
            'password' => 'secret',
        ]);

        $this->token = $response->json()['access_token'];

        Auth::login($this->user);
    }

This works without a problem, the only thing is it takes a lot of performance, 20 tests take about 10 seconds to execute since this is happening for every test.

I was thinking of creating a helper class and using PHPUnit's static method setUpBeforeClass, but if I do that, $clientRepository is missing context and everything fails.

Any ideas?

Edit Your Profile
Update

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