Friday, July 8, 2016

Laravel 5.2 and MySQL dates

Hi

Today we will learn, how to use MySQL dates with laravel 5.2.

Starting with MySQL 5.7, 0000-00-00 00:00:00 is no longer considered a valid date, since strict mode is enabled by default. All timestamp columns should receive a valid default value when we insert records into our database. We may use the useCurrent method in our migrations to default the timestamp columns to the current timestamps, or we may make the timestamps nullable to allow null values:

$table->timestamp('foo')->nullable();

$table->timestamp('foo')->useCurrent();

$table->nullableTimestamps();

Thanks

Friday, June 24, 2016

How to specify a Node version on a project basis

Hi

Today we will learn, how to manage different node version on project basis.
Suppose we have 2 different project which uses different node version. One is using node 5.0 and another is using 6.0. How we manage that.

This is kind of possible with nvm in that if we create a .nvmrc file inside a project and specify a version number, we can cd into the project directory and type nvm use. nvm will then read the contents of the .nvmrc file and use whatever version of Node we specify.

cd into project1 directory.
touch .nvmrc # creates the file
echo \5.0 >> .nvmrc # appends the version to the file

now run command

nvm use 5.0 or nvm use current

it will show

Now using node v5.0.0 (npm v3.3.6)

now cd into project2 directory.
touch .nvmrc # creates the file
echo \6.0 >> .nvmrc # appends the version to the file

now run command

nvm use 6.0 or nvm use current

it will show

Now using node v6.0.0 (npm v3.8.6)

This is how we can manage different version of node for different project.

Thanks

Friday, June 17, 2016

Server Monitoring Command for Laravel

Hi

Today we will learn server monitoring command for laravel.

Server Monitoring is a package that will periodically monitor the health of our server and website. It provides healthy/alarm status notifications for Disk Usage, an HTTP Ping function to monitor the health of external services, and a validation/expiration monitor for SSL Certificates.

This package works by setting up a config file and then having a monitor:run artisan command set on a schedule. When it runs it will alert us via email, Pushover, Slack, or logged to the filesystem.

It currently supports the following monitors:

Disk Usage Monitors
Disk usage monitors check the percentage of the storage space that is used on the given partition, and alert if the percentage exceeds the configurable alarm percentage.

HTTP Ping Monitors
HTTP Ping monitors perform a simple page request and alert if the HTTP status code is not 200. They can optionally check that a certain phrase is included in the source of the page.

SSL Certificate Monitors
SSL Certificate monitors pull the SSL certificate for the configured URL and make sure it is valid for that URL. Wildcard and multi-domain certificates are supported.

The monitor will alert if the certificate is invalid or expired, and will also alert when the expiration date is approaching. The days on which to alert prior to expiration is also configurable.

You can find out more about this package on Github.

Friday, June 10, 2016

Laravel 5.3: Rollback one migration

Hi
A new feature has been added to Laravel 5.3 that will allow us to back out a single migration:
php artisan migrate:rollback --step=1
This is great for when we migrate and it runs through a batch but we want to back out just the last one instead the whole batch.
project/folder$ php artisan migrate
Nothing to migrate.

project/folder$ php artisan migrate:rollback --step=1
Roller back : 2016_06_10_500_last_created_table
 Thanks

Saturday, May 28, 2016

Laravel 5 : How to check current URL or Route

Hi

Today we will learn how to check or get current url or route.

Sometimes we need to check current URL or route and do some action.

1. Check if URL = myurl

Simply – we need to check if URL is exactly like myurl and then we show something.

In Controller:

if (\Request::is('myurl')) {
  // do some thing
}

In Blade file – almost identical:

@if (\Request::is('companies'))
  // do some thing
@endif


2. Check if URL contains myurl

A little more complicated example – method Request::is() allows a pattern parameter, like this:


if (\Request::is('myurl/*')) {
  // will match URL /myurl/999 or /myurl/create
}


3. Check route by its name

As we know, every route can be assigned to a name, in routes.php file it looks something like this:

Route::get('/myroute', ['as' => 'myrt', function () {
  return view('myroute');
}]);

So we can check if current route is myrt

if (\Route::current()->getName() == 'myrt') {
  // We are on a correct route!
}

So these are three ways to check current URL or route.

Thanks

Friday, May 20, 2016

Laravel 5 Validation rules "sometimes"

Hi

Today we will discuss validation rules "sometimes", apply rules only if field exists.

Imagine that we have two forms – one with username/password fields and another with email/password fields. And we want to have the same validation rules (either in Request, or directly in Controller, doesn’t matter).

And we need to validate if the email is filled in ONLY if that field is present within the form. Then we call it ‘sometimes’.

$this->validate($request, [
  'email' => 'sometimes|required|email'
]);

To understand better, Let’s look at the difference between this code and the “simple” one:

$this->validate($request, [
  'email' => 'required|email'
]);

The second validation will fail if the email field is empty or is not in the form at all. That’s the key point – the first validation will fail only when the field is present but empty.

It is simple but I hope that it will help someone.

Thanks

Friday, May 6, 2016

How to Log every request & response in Laravel 5

Hello Everyone,

Today we will discuss about how to log every request & response in Laravel 5.1.

Sometimes it's useful to log some/all requests to our application. This is really convenient when we use Laravel to build our APIs.

A logging middleware might log all incoming requests to our application. In Laravel 5.1 there is a terminate method and it's call after the sending HTTP response to the browser. This way we have access to both $request and $response at the same time.

Let's take a look at how we are going to achieve this in Laravel 5.1:

Create a new middleware by typing this command in terminal inside our project directory.

php artisan make:middleware LogAfterRequest

And then put below code in it(project_dir/app/Http/Middleware/LogAfterRequest.php).

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class LogAfterRequest {

    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        Log::info('app.requests', ['request' => $request->all(), 'response' => $response]);
    }

}

In this code terminate method receives $request and $response. These are objects which will give us all of the handy stuffs we probably need to log.

When we have our middleware ready, we should add it to our HTTP Kernel.

Open Kernel.php and add this line to our protected $middleware property:

\App\Http\Middleware\LogAfterRequest::class

That's it. We can additionally filter what we want to actually log, but this is the basics.

Thanks.