Creating a new project in Laravel

Generating a Laravel 5.8 project is easy and straightforward. In your terminal, run the following command:

$ composer create-project –prefer-dist laravel/laravel laravel-first-project-app

This will install laravel/laravel

Installing front-end dependencies

In your generated project, you can see that a package.json file is generated which includes many front-end libraries that can be used by your project:

axios,
bootstrap,
cross-env,
jquery,
laravel-mix,
lodash,
popper.js,
resolve-url-loader,
sass,
sass-loader,
vue.

You need to use npm to install the front-end dependencies:

$ npm install

After running this command a node_modules folder will be created and the dependencies will be installed into it.

Creating  MySql Database:

In your terminal, run the following command to run the mysql client:

$ mysql -u root -p

Now run the following SQL statement to create a db database:

mysql> create database db;

Open .env file and update the credentials to access your MySQL database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=******

Here you need to enter the database name, the username and password.

At this point, you can run the migrate command to create your database and a bunch of SQL tables needed by Laravel:

$ php artisan migrate

MVC Structure:

Laravel uses the MVC architectural pattern to organize your application in three decoupled parts:

The Model which encapsulates the data access layer,
The View which encapsulates the representation layer,
Controller which encapsulates the code to control the application and communicates with the model and view layers.

Creating Laravel Model:

$ php artisan make:model Book –migration

This will create a Contact model and a migration file. In migration we can add fields that we need in books database table

Controllers And Routes:

$ php artisan make:controller BookController –resource

Open the app/Http/Controllers/BookController.php

Here you can see BookController class extends Controller class available from Laravel and defines a bunch of methods which will be used to do the CRUD operations against the Book model.

Now we need to route for this controller

Open the routes/web.php file and update it accordingly:

<?php
Route::get(‘/’, function () {
return view(‘welcome To The Library’);
});

Route::resource(‘books’, ‘BookController’);

Using the resource() static method of Route, you can create multiple routes to expose multiple actions on the resource.

These routes are mapped to various BookController methods which will need to implement in the next section:

GET/books, mapped to the index() method,
GET /books/create, mapped to the create() method,
POST /books, mapped to the store() method,
GET /books/{book}, mapped to the show() method,
GET /books/{book}/edit, mapped to the edit() method,
PUT/PATCH /books/{book}, mapped to the update() method,
DELETE /books/{book}, mapped to the destroy() method.

These routes are used to serve HTML templates and also as API endpoints for working with the Book model.

Return Type declarations in PHP7

PHP 7 also supports Return Type Declarations which support all the same types as arguments. To specify the return type, we add a colon and then the type right before the opening curly bracket.

function getSum(float $a, float $b) : float {

Adding the return type allows you to to be sure your function returns what is expected as well as making it easy to see upfront how the function works.

Continue reading

PHP7 feature (??)

The Null Coalescing operator is denoted with two question marks ( ?? ). You can use it when you want to check if something exists and return a default value in case if it doesn’t exists.
No need to use long methods ‘isset’ or if the variable not equals to null. Simple use (??)

Example:

$action = $_GET[‘action’] ?? ‘noaction’;

Integrating Google Recaptcha in PHP

Captcha is one of the best ways to protect any form spammers. Captcha helps websites to distinguish between computer and humans. Usually, captcha comes a picture which has some text written. The user needs to enter the text written on the image in captcha input field & the field has to be validated on the server. The text written on the image is somewhat difficult to read and in the random pattern so that machines can not read. Although this is a good way to verify the human, but it is little frustrating for the valid users. To overcome with the frustrating experience Google has introduced No Captcha reCaptcha.

Please follow steps below to integrate Google recaptcha in your website:

Continue reading

PHP Namespaces

Namespacing does for functions and classes what scope does for variables. It allows you to use the same function or class name in different parts of the same program without causing a name collision.

In simple terms, think of a namespace as a person’s surname. If there are two people named “Peter” you can use their surnames to tell them apart.

Example:

Suppose you write an application that uses a function named output(). Your output() function takes all of the HTML code on your page and sends it to the user.

Continue reading