Categories
Fixing Stuff Laravel Samuel

Fixing Laravel Localhost Email Issue

While updating a clients site today we ran into an error message when trying to do a test on a submission for a new contact form that was created, the error message read:

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html

To Fix This Issue we Completed the Following Steps:

I am using PHP 7.1.9 for my WampServer, so change 7.1.9 in the example below to the version number you are currently using.

;curl.cainfo

  • Change it to:

curl.cainfo = "C:\wamp64\bin\php\php7.1.9\cacert.pem"

  • Make sure you remove the semicolon at the beginning of the line.
  • Save changes to php.ini, restart WampServer, and you’re good to go!

That fixes our issue with the curl error if we are using localhost to server our site, but if we are using php artisan server to dispaly the webpage then we also need to complete the steps below:

If your laravel project is located in c:\wamp\laravel\ then you should be able to call http://localhost/laravel/public and see the default laravel welcome page or your own.

  1. Open your /app/config/app.php file and check that the value of url directs to your project. 'url'=>'http://localhost/laravel/
  2. Then open your .htaccess file and add the RewriteBase rule under the RewriteEngine On Line RewriteBase /laravel/public/

Credit for these answers goes to:

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']);