SOFTonSOFA

Author: Jarek (page 1 of 3)

Too much M4g1c will kill you (or at least bite your ass)

freddie

So, as you know, I’m a big fan of Laravel, in particular Eloquent is my favourite.
The beauty and value of both is a lot of conventions and magic happening behind the scenes. That magic, when balanced, allows us focus on things that really matter, rather than taking care of boilerplate and tedious, repetitive tasks, we’ve all been doing so many times. However…
Continue reading

Tinker like a boss – WTF?!

This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Errors – WTF?

Usually getting verbose errors in tinker is now what you want. You made a typo, called wrong method etc – all feedback you need then is a short message and you can fix your error. Sometimes, however, you may want to actually see more information about the errors or full stacktrace behind it.
Continue reading

Tinker like a boss – helpers

This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Helpers

You may find yourself in need of some handy shortcuts when tinkering. I often need to make an HTTP call or run a quick test request, that works exactly as the one you’d run in your Laravel tests.
Continue reading

Tinker like a boss – aliases

This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Aliases

Class aliasing is very simple concept, that allows you to call a class by its alas, rather than original name. It’s not very common to use aliases when writing code , for your IDE gets you covered.

If you’re working with Laravel, it’s worth mentioning that all the facades are also aliases, that’s why you have things like request, validator etc available in global namespace. this is a convention introduced by Taylor to make things a bit more expressive,but still something that your IDE does for you.

Continue reading

Tinker like a boss – custom casters

This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Custom casters

Using Laravel, you’re already familiar with idea of casters even if you’re not aware of that. Check this out:

Continue reading

Tinker like a boss – custom includes

This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Custom includes

Let’s stick to a convention of a .tinker file bootstrapping our REPL env. PsySH allows including such file in 2 ways:

Continue reading

they can bite! – how (not) to use accessors in Eloquent

snake

Eloquent mutators & accessors are handy feature that can simplify your high level code and hide some data-related logic in the model.

However, it is pretty common to misuse them and soon regret it.

Let’s take a look and see where we can go wrong…

Continue reading

Querying relations in Laravel: get Models where latest related is…

tools

Another part of Querying relations in Laravel will cover such problem:

I want to get SomeModels that have latest RelatedModel (hasMany) matching.. _SOME CONSTRAINT HERE_

Eloquent provides expressive syntax for querying relations that can be used for simple cases: SomeModel::whereHas('relatedModels', function ($q) { ... })->get(). Unfortunately it is not capable of handling the latest RelatedModel part just like that.

Let’s dive into how we can tackle this problem using 2 approaches:

 

  1. The easy way – using Collection methods
  2. The right way – using subqueries

Continue reading

PHP how to use $this in Closure – context matters

closure

Anonymous functions, in php known as Closures, come in handy very often. One application in particular is very useful – extending classes, thanks to the capability to bind the closure to an instance and class scope.

However, binding to instance may prove a bit tricky, so let’s go through it together.

Continue reading

Olderposts

Copyright © 2017 SOFTonSOFA

Theme by Anders NorenUp ↑