Categories
Fixing Stuff Laravel Samuel

NPM Install Error On Laravel Project

When Running npm install we were getting the following error:

npm ERR! cb() never called!

To Fix this we ran:

npm cache verify

Then We Re-Ran npm Install and everything installs successfully 🙂

Categories
Fixing Stuff Laravel Samuel

Update Laravel Column To Allow NULLABLE Entries

We had a client request today that 2 fields on their user creation page be updated to allow for NULL entries. Once we updated the site to Laravel 5.6 the new stricter settings were throwing errors with these blank input fields. Luckily in our case both columns did not have any data in them, so this made our change relatively straightforward. Jumping into it we first created new migrations for both columns:

//Update column to allow null entries

/**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('historical_overtime', 32)->nullable()->after('shift_code');;

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('historical_overtime');
        });
    }

From Here we pulled up phpMyAdmin and dropped our old columns using the code:

ALTER TABLE users DROP COLUMN middle_name;
ALTER TABLE users DROP COLUMN historical_overtime;

We then deployed our new migration files to our the live server, ran them to install the new DB columns and away goes our error. For future reference the error code we fixed with this is:

SQLSTATE[2300]: Integrity constraint violation: 1048 Column 'middle_name' cannot be null

If the DB columns would have had information in them, instead of deleting them we could have updated them using the code below:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        // change() tells the Schema builder that we are altering a table
        $table->integer('middle_name')->unsigned()->nullable()->change();
    });
}
Categories
Fixing Stuff Laravel Samuel

Laravel – Sorting A Date Time Column By Month/Day and Excluding Year

We had an interesting request come in from a client today. They have a big list of user birthdays that they want to display on the front end of the ir employee intranet site. In order to protect employees actual age they only want the month and day of the employees birthday to show, that way people can essentially wish them happy birthday but not know their true age. In essence the client wanted a list of birthdays like this:

Employee 1 12/1
Employee 2 12/4
Employee 3 12/5

To add a little complexity to this task the client wanted us to sort the birthdays by day so that they were listed in chronological order. Sorting this list is pretty easy with the orderBy command, this is what we came up with first:

$birthdays = User::where('birth_month', $current_month)->where('active', 1)->orderBy('birthdate', 'asc')->get();

The issue we ran into with the code above is that it sorted by year, then day. Even though we are not showing the year on the front end, it is still saved in the database as a full date/time string of day-month-year. The code above ended up giving us a list like this:

Employee 1 12/4/1990
Employee 2 12/1/1992
Employee 3 12/5/1992

Any employees who had the same birth year were grouped together and then sorted by date. To fix this we had to change from orderBy to orderByRaw. In practice the code we ended up using looks like this:

$birthdays = User::where('birth_month', $current_month)->where('active', 1)
->whereMonth('birthdate', $current_month)
->orderByRaw('DATE_FORMAT(birthdate, "%m-%d")')
->get();

The code above ended up working perfectly for the client. You will see that the orderByRaw code above uses the m-d portion of the date/time string but it does not use the year portion. With this code we are left with a list like this:

Employee 1 12/1/1992
Employee 2 12/4/1990
Employee 3 12/5/1992

Categories
Laravel

Setting Up Laravel Staging Environment

In this post we are going to cover setting up a Laravel staging environment. We are going to assume that a live environment has been setup on this account following these steps previously covered.

1.) Create A new folder for the staging site, this will live in /git/staging/repo-name

2.) Manually Clone The repo into this new staging folder then checkout the staging branch

3.) Create A symlink to this folder for the staging subdomain:
ln -s ~/git/staging/repo-name/public ~/public_html/staging

*Please note, in order for this to work symlinks to the files in the live sites public folder must be setup as symlinks instead of setting up the entire public_html folder as a symlink to the lives sites /public folder. In practice the code to do this looks like:

ln -s ~/git/live/repo-name/public/css ~/public_html/css
ln -s ~/git/live/repo-name/public/fonts ~/public_html/fonts
ln -s ~/git/live/repo-name/public/images ~/public_html/images
ln -s ~/git/live/repo-name/public/js ~/public_html/js
ln -s ~/git/live/repo-name/public/.htaccess ~/public_html/.htaccess
ln -s ~/git/live/repo-name/public/favicon.ico ~/public_html/favicon.ico
ln -s ~/git/live/repo-name/public/index.php ~/public_html/index.php
ln -s ~/git/live/repo-name/public/robots.txt ~/public_html/robots.txt

4.) Create a staging subdomain with document root of public_html/staging

5.) Create new database and add credentials to .env file

6.) run migrations and seeds

Categories
Laravel Samuel

REgistering Middleware

After floundering with creating custom middleware today I wanted to type up my notes for future use. I apologize about the lack of polish these are for my own personal user later:

1.) Create new middleware file: php artisan make:middleware FileName

2.) Add whatever rules are needed to this file (note see SFPS or Fire for great examples)

3.) Register your middleware. Open up app/Http/Kernel.php and find the following section:

protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

*Just toss your new middleware in the mix, so an example I just added is:
'auth_administrator' => \App\Http\Middleware\AuthAdminister::class,

4.) Add your route group to web.php file, something like:

Route::group(['middleware' => 'auth_administrator'], function () {

// include all the routes you want to protect

});

5.) If you need to redirect a customer to their specific page then create this new page view and route, something like:
Route::get('/customer-dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@customer']);

Categories
Laravel Samuel

Animating Content Using AOS

We went on the hunt for a lightweight package to animate content into a Laravel project today. We found what looks to be a great package that we are trying out now, will report back findings and notes after using:

AOS Github Page:
http://michalsnik.github.io/aos/

There is a great ReadMe file located here:
https://github.com/michalsnik/aos/blob/next/README.md