Did you know? You can fetch multiple Eloquent records with one command

The Eloquent class in Laravel has a method called find, this you must be aware of , you can use this to find an Eloquent Model record using the primary key. This is used as follows

// Retrieve a model record by its primary key...
$car = App\Car::find(1);

Now, what if we want to find all cars with primary key in the list [1,2,3]? You can suppluy an array of primary keys to the find method or use the findMany method. Consider the code snippet below

$cars = App\Car::find([1, 2, 3]);
or
$cars = App\Cr::findMany([1, 2, 3]);

 

Note: If you supply an array of keys to the find method, it calls findMany internally. Whichever you decide to use between the two methods described above, they both return an instance of  \Illuminate\Database\Eloquent\Collection.

giphy5

 

 

Advertisements

Highlighting Active Bootstrap Menu Items (Nav – Item) Based On Route

In this post, I will show you a niifty little trick, one that is always required whenever you are working on some sort of web project with multiple routes.

You occasionally need to set the nav-item that inidicates the current route to be active (i.e. make it coloured ), to achieve this using laravel, consider the code snippet below

<ul class="navbar-nav">
<li class="nav-item {{ is_active('route.name.one') }}">
    <a class="nav-link" href="{{ route('route.name.one') }}">Route One</a></li>
<li class="nav-item {{ is_active('route.name.two') }}">
    <a class="nav-link" href="{{ route('route.name.two') }}">Route Two</a></li>
<li class="nav-item {{ is_active('route.name.three') }}">
    <a class="nav-link" href="{{ route('route.name.three') }}">Route Three</a></li>
</ul>

As you can see from the snippet displayed above, for every <li> element, the is_active function call has been added to class attribute , this function takes the name of a route as its argument and it returns a class name (active) if the current route name matches that which was supplied. Consider the snippet below to see the logic in the function stored in a file called helpers.php in a new folder called custom (note:  you can store your helper file anywhere you deem logical, I have just chosen custom/helpers.php).


if (!function_exists('is_active')) {
    /**
     * @param string $routeName
     * @param string $activeClassName
     *
     * @return string
     */
    function is_active(string $routeName, string $activeClassName = 'active')
    {
        $result = '';
        if (request()->route()->getName() === $routeName) {
            $result = $activeClassName;
        }

        return $result;
    }
}

How do you make your function available globally?

To make the function in the [core/helpers.php] file available globally, you need to autoload it using the files attribute of the composer.json file as follow.

"autoload": {
    "classmap": [
      "database/seeds",
      "database/factories"
    ],
    "psr-4": {
      "App\\": "app/"
    },
    "files": [
      "core/helpers.php"
    ]
  }

Hope you have found this helpful..

How to extend the laravel Collection with a macro

At this point in time, if you are familiar with the Laravel framework, I am sure you must have used the lovely Collections class. It is one of my favourite features of the laravel framework, so much so that I use in some bespoke projects outside laravel.

What is Laravel Collections?

It is nothing but php arrays on steroids. It allows you to perform very cool manipulations and processing of php arrays.

How do you create a collection?

$collectionInstance = collect([1,2,3,4,5,6,7]);

$collectionInstance2 = \Illuminate\Support\Collection::make([1,2,3,4,5,6,7]);

The collection class has a hosts of methods , such as

  1. avg
  2. sum
  3. unique
  4. and a lot more

However, sometimes you wish to use a method on the collection class and it isn’t available, so how do you do it? Collection Macros to the rescue.

Creating a macro to convert all items of a collection to camel case.

Consider, if you wish to convert all the strings in a collection to their respective camel case values, and there isn’t one method to do this. Obviously you can use a map or transform to achieve this, but for convenience sake, lets create a macro.

 Collection::macro('camelise', function(){
    return $this->map(function ($value) {
        return camel_case($value);
     });
 });

// to use it
$items = ['foo_bar', 'hello_world'];
$expectedValues = ['fooBar', 'helloWorld'];
$camelValues = collect($items)->camelise();

// assert that result matches the expected values.
$arraysAreEquals = $expectedValues === $camelValues->toArray();
assert($arraysAreEquals);

 

Where do I put the macros?
As helpful as this is, imagine if you have a 100 of such methods, where do you keep them?

Now this is a subjective answer, you can extract your methods into files and require them from a loop of the glob of the location of these files or you create a class with your macros and you use the Php Reflection Class to loop through them and add them to the Collection class.

Also, the AppServiceProvider will be a good point to add the logic described above.

 

giphy4

Fetching Laravel Eloquent results ordered by oldest to newest

In the previous post, I showed you how to order you results from newest to oldest using Eloquent model.

It is only logical to show you how to reverse the operation, ordering you results from the oldest to the newest. As always, Laravel in its majesty has already created a method for this and guess what it is called? oldest, so obvious right?

Consider the code below

Post::oldest()->get();

This will return the results already ordered by created_at from oldest to newest.

giphy3

Fetching Laravel Eloquent results order by newest to oldest

This tiny requirement is one that keeps popping up in you day to day programming routine, yet it is greatly misunderstood at least in the laravel way.

Imagine, if you have a model called Post (mapped to table named posts) and you have been asked to list the posts in descending order of creation date (created_at). Solving this using the ever reliable raw sql can be done as follows

select * from posts order by created_at desc

Wrong way

A very forgivable but wrong approach is to get the Posts and then order It

 Post::get()->sortByDesc('created_at');

While this would work, it is inefficient and in some cases may even be wrong, it is mainly inefficient because it fetches then entire Posts results before ordering it. This may be very expensive as you can imagine depending on the size of the posts table.

Right way (Long route)

The right way is to add the ordering condition on the Post model before getting it. i.e

 Post::orderBy('created_at', 'desc')->get();

As you can see, this is different from the wrong way because it orders the resultset before getting it.

Right way (Short route)

Although we have a right way of achieving this using the method described above, laravel is known for syntactic sugar and guess what, there is one for doing just this. It is called latest. Consider the code below

 Post::latest()->get();

giphy2

Dont you enjoy seeing the gibberish? Using strace for debugging on linux

In this quick post, I will introduce to my nippy little friend and one of my favorite tools debugging tools on linux. so, what is strace?

According to wikipedia, strace can be defined as “a diagnostic, debugging and instructional userspace utility for Linux”.

Sometimes, you are pulling you hair out debugging your php application through the php-fpm or maybe your are looking into the apache end of the stack and you really wish you can see what is going on under the hood because as far as you can see after checking the configuration files and you code, everything should work just fine. If only there was a tool that would allow you see the exact instructions that your php-fpm or apache processes are issuing.

giphy

Strace to the rescue

How do you use strace? just follow the steps listed below

  1. Find the process Id of the process you wish to listen on (i.e. watch a real time log of all instructions issued by the process)
  2.  Attach the process Id to strace

Find the process Id

Here, the ever reliable ps tool will come in handy, use it as follows
ps-example

from the screenshot above, you can see the process ids for the process spawned by apache, if you identify the one you wish to debug, just note the process id (e.g. 22179) and attach strace to it.

Attaching strace to the process.

To attach strace to the process id, just issue the command below

$ sudo strace -p 22179

note: 22179 is the process id that you noted from the ps -aux command

View a sample output of running the strace command below

strace-output

Very cool right?

Note: you can attach strace to any process whatsoever, mysql, php-fpm, rabbitmq etc.

giphy1

How to get your private and public OpenSSH keys from a putty key

The .ppk format for private keys created by puttyGen  for use with pageant is very portable for sharing and as such, you find yourself moving it around often. This then usually leads to a problem , which is converting it to the OpenSSH format that is required by most tools you wish to integrate with.

To, export your .ppk to the OpenSSH format and also get copies of your private and public key, follow the steps listed below.
puttygen supports exporting your private key to an OpenSSH compatible format. You can then use OpenSSH tools to recreate the public key.

  1. Open the tool PuttyGen and do the following
    – Click Load and then select your .ppk file
    –  Click the Conversions menu and then click Export OpenSSH Key
    – Save the key with any filename you wish  (usually ~/.ssh/id_rsa)
  2. Use ssh-keygen to get the RFC 4716 version of your public key from the exported private key using the command given below
    ssh-keygen -e -f ~/.ssh/id_rsa > ~/.ssh/id_rsa_com.pub
    
  3. Use ssh-keygen to convert the RFC 4716 version of the public key to the OpenSSH format:
    ssh-keygen -i -f ~/.ssh/id_rsa_com.pub > ~/.ssh/id_rsa.pub