SOFTonSOFA

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

array_merge vs array_replace vs + (plus aka union) in PHP

join

If you wrote some PHP, most likely you have used array_merge here and there for your arrays. You may have met array_replace function, introduced in PHP5.3. Finally, you could notice + operator (aka union).

Since all 3 do similar thing, and the docs don’t quite describe the difference between them, here’s a nice image of it

Continue reading

Revisionable – a quick example

A short reminder about my revisionable package (compatible with L4 & L5+) and quick example of how you can use it:

Zgłoszenia

Continue reading

Laravel Searchable – the best package for eloquent

search

In this first post of a series about https://github.com/jarektkaczyk/eloquence – the package that allows working with eloquent models in even simpler manner – I’m going to introduce you to the searchable feature of the builder class.

Continue reading

« Older posts

Copyright © 2016 SOFTonSOFA

Theme by Anders NorenUp ↑