Laravel collections merging peculiarity

Laravel collections merging failings

Laravel Eloquent collections merging fails, if initial collection has same ‘id’ as any of being merged.

Lets assume we have these Eloquent collections:

Lets assume, that $cars and $bikes have ‘id’ element with same value ‘1’.

In such case, if we do this:

… we will get elements with same value overwritten, loosing one collection element.

Here is workaround:

You create empty collection and use it as initial collection, to which you merge all others.

In this case, since your initial collection has no elements, it cannot conflict with any following collection and you get no overwrites

Laravel Macros

Laravel 5: Macros

You can add functionality Laravel does not have out of the box by using Macros

Macro is defined in service provider.

I created a separate one for macros.

Note: macro takes argument, like this:

Now, we can use it in overriden in \app middleware CheckForMaintenanceMode and allow people connecting from some IPs (like some developer), to have access to a system in maintenance, thus being able to fix things:

Here is what’s macroable:


  • Cache
  • File
  • Lang
  • Request
  • Response
  • Route
  • URL

Illuminate Classes

  • Illuminate\Cache\Repository
  • Illuminate\Console\Scheduling\Event
  • Illuminate\Database\Eloquent\Builder
  • Illuminate\Database\Eloquent\Relation
  • Illuminate\Database\Query\Builder
  • Illuminate\Filesystem\Filesystem
  • Illuminate\Foundation\Testing\TestResponse
  • Illuminate\Http\RedirectResponse
  • Illuminate\Http\Request
  • Illuminate\Http\UploadedFile
  • Illuminate\Routing\ResponseFactory
  • Illuminate\Routing\Router
  • Illuminate\Routing\UrlGenerator
  • Illuminate\Support\Arr
  • Illuminate\Support\Collection
  • Illuminate\Support\Str
  • Illuminate\Translation\Translator
  • Illuminate\Validation\Rule



Lang translation in controller, model …

Lang translation in controller, model …

How to get language translation inside of controller, model etc.


Cookies and View

Cookies and View

See: this thread

way #1: queue (seems best)

way #2: use CookieJar

way #3: use response


Get redirect back link

Get redirect back link

Why would you want this?

E.g. you are in ‘edit’ page you got to from listing.
You want to have redirect link back exactly to page in listing all with proper page number and sorting.
You could use:

… but above would also pickup all JS activity, you may not want, like Toastr notifications for example.

If you need a link to do back and not just a redirect back e.g. in controller, here are options:

And solutions that work with cloud/cluster hosting:

Get current language

Get current language

Here’s how to access current language code (pl, en, nl-be etc.) in Laravel 5:

Of course, except for ‘example #1’, you have to ‘use’ appropriate objects.


Check number of queries run

Check how many queries Laravel runs.

Sometimes you mat want to see how many queries Laravel has just run.

It may come handy, when using ‘eager loaded’ relations.

Just to make sure, if you are really ‘eager loading’ stuff.

Also, it is handy to see query as it was passed to SQL server in raw SQL code.

Of course, we have helper function toSql(), but in some cases this method is better:

And this is a result you will get on the screen:

Above example shows, that we have run 2 queries.

Of course you can accomplish this in many different ways, including systemic.
But I prefer surgical placement of testing tool, where I need it.

You can also read more here: