Wouldn’t it be cool if there was a history of the model in our project?

Yes of course! You only need to add the HasHistory Trait and the HasHistoryInterface Interface to the choosen model.

You will receive the history of actions performed on a given model in an easily accessible way:

  • created
  • updated
  • deleted
  • restored

A user is assigned to each action, if logged in. This means that you can receive full information that a specific user has performed on tracked models.

The exact installation is in the repository.

https://github.com/rudashi/laravel-history/

--

--

When creating an API, it is sometimes necessary to know if our code is not doing any unnecessary SQL queries.

These few lines of code allow queries to be written to the /storage/logs/query.log

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Database\Events\QueryExecuted;

private function queryListener(): void
{
DB::listen(function (QueryExecuted $q) {
File::append(
storage_path('/logs/query.log'),
$q->sql.' ['.implode(', ', $q->bindings).']'.PHP_EOL
);
});
}

Just put the method in ServiceProvider (eg. AppServiceProvider) and call it in the boot() method.

--

--

Quick Trick to anyone who had the same dilemma as me.

How to get value from collection with one-to-many relationship ?

The solution is really simple. Use Dot notation !

Post::with('users:id')->get()->pluck('users.*.id')->collapse();

Simple and elegant.

--

--