Why people choose laravel framework

Laravel is one of the most popular PHP frameworks among the web artisans. Laravel is  the latest web app framework with this framework you can make web  development rapid and easy by by enabling general tasks that will be used in the majority of web projects such as queue, sessions, caching and authentication.

Why people choose laravel


It comes with a rich set of features. Using the resource controllers, CRUD process is very simple. The eloquent ORM helps in intracting with the databases. Every model developed in the MVC structure correlates to a table in the database.
Laravel have the expressive and elegant syntax that has been designed to ease and speed up the web development process. It has a vibrant developer community over the internet.


There are many reason behind for choosing the laravel framework because it incorporates established and proven web development patterns, MVC Application structure and Active record powering its database wrapper.It helps a developer to build a maintainable web application with easy to understand code separation in a short time frame.

Modular : Laravel is built on more than 20 different libraries and is itself split up into individual modules. It has adopted modern PHP principles which allow developers to build responsive, modular and user friendly apps.

Libraries : It supports many development environments and adjust itself based on the appropriate platform where app is running.

Template Engine : It is very lightweight templates with which you can create amazing layouts with dynamic content seeding. It has many widgets having JS and CSS code with solid structures.

Quickly : The PHP development companies have adopted Laravel due to its best dynamic capabilities to handle big web development projects.

Open Source : It is an open-source PHP web application framework, that makes easy and free process for building complex and large web applications with code maintainability.

How to send mail using Node.js

In this post i  will show you how to send­ emails using G-Mail SMTP in Express(framework) Node.js.Sending a email using express js is pretty easy in node.js there are many library available for sending a mail.

Send mail using node.js express


Step 1: We are using express-mail libary for sending a mails and pugjs is a template engine which we are using for this post.

Step 2: Create a package.json file in your folder.


{
  "name": "send email using node.js",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: something going wrong\" && exit 1"
  },
  "keywords": [
    "express",
    "mailer"
  ],
  "author": "wantcode",
  "dependencies": {
    "express": "^4.14.0",
    "pug": "^2.0.0-beta6",
    "express-mailer": "^0.3.1"
  }
}

or you can directly genreate package.json using following commands.


npm init //initialize the npm 
npm install --save pug express express-mailer // install express mailer and pug dependencies

Step 3: Now come to main file called server.js.


// call the packages we need
const express = require('express');  // call express
const mailer = require('express-mailer'); // call express
const app = express(); // create a server
 
const port = process.env.PORT || 8000;  // set our port
 
// set the view folder to views
app.set('views', __dirname + '/template');
// set the view engine to pug
app.set('view engine', 'pug');
 
// Configure express-mail and setup default mail data.
mailer.extend(app, {
  from: 'info@wantcode.in',
  host: 'smtp.gmail.com', // hostname
  secureConnection: true, // use SSL
  port: 465, // port for secure SMTP
  transportMethod: 'SMTP', // default is SMTP. Accepts anything that nodemailer accepts
  auth: {
  user: 'youremail@gmail.com', // gmail id
  pass: 'yourGmailPassword' // gmail password
  }
});
 
 
// test route to trigger emails
app.get('/', function (req, res) {
  // Setup email data.
  var mailOptions = {
    to: 'imsonujangra@gmail.com',
    subject: 'Email from SMTP sever',
    user: {  // data to view template, you can access as - user.name
      name: 'Sonu Jangra',
      message: 'Hello welcome to my website wantcode'
    }
  }
 
  // Send email.
  app.mailer.send('email', mailOptions, function (err, message) {
    if (err) {
      console.log(err);
      res.send('There was an error sending the email');
      return;
    }
    return res.send('Email has been sent!');
  });
 
});
 
app.listen(port, function () {
  console.log("Example app listening on port ${port}!");
});

Step 4: Create a view called template/email.pug.


doctype html
head
body
  div
   p
    b Hi #{user.name}
  div
    p #{user.message}

Step 5: Run the app using node app.js

Check the action on browser using  : http://localhost:8000/

Git ignore file permission changes

Sometime you change the mode of files of your project.

Git chmod



sudo chmod -R 777 /var/www/project

But files are not modified in the repo. check the modified files in git status. so git show all these files are modified.

So you can remove these files from modified using this command.


git config core.fileMode false


   core.fileMode
       If false, the executable bit differences between the index and the
       working copy are ignored; useful on broken filesystems like FAT.
       See git-update-index(1). True by default.

git -c core.fileMode=false diff

And the --global flag will make it be the default behavior for the logged in user.

git config --global core.fileMode false

I hope this post will help you.

ANDROID_HOME environment variable in ubuntu?

In this post i will show you how to set the ANDROID_HOME environment in ubuntu.Sometime after installing android studio you have noticed this issue of warning.

ANDROID_HOME environment



The ANDROID_HOME environment variable is not set or it points to a non-existent directory.
You will not be able to perform any build-related operations for Android.

So download the android and get the android download folder path like.

/home/wantcode/android

In this above line where wantcode is my system username and android is the folder where i downloaded android tools.So you can set the android path like.


export ANDROID_HOME=/home/wantcode/android/
export PATH=$PATH:$ANDROID_HOME:/home/wantcode/android/tools/
export PATH=$PATH:$ANDROID_HOME:/home/wantcode/android/platform-tools

After setting you can try to print the android path like.


echo $ANDROID_HOME


Connect your mysql database with node.js

Node.js is popular framework today and real time things is possible with the node.js like real-time chat notification etc.So today i am going to show you how to connect node.js with mysql database.Node.js have a driver for MySQL that way we can easily connect with database and you can use select, insert, update and delete sql query.

Mysql Node.js


So lets start :

Step 1: Installed node.js if it is not installed in your system. 

sudo apt-get update
sudo apt-get install nodejs

You can read this post for installing the node.js from scratch

Step 2: Install npm for package management support.


sudo apt-get install npm

Now install the mysql package for node.js


sudo npm install mysql

I'm supposing you have installed php , mysql in your system and we are using node(database) if you don't have a database with name node then create it and inside it a users table .

Ok, so create new file server.js and put bellow code inside that file:

Step 3 : Ceate a new file.


var mysql      = require('mysql'); 

var connection = mysql.createConnection({ 
  host     : 'localhost', 
  user     : 'root', 
  password : 'root', 
  database : 'node' 
});

connection.connect(); 

connection.query('SELECT * FROM users', function(err, result, fields)  
{ 
  if (err) throw err; 

  console.log(result); 
}); 

connection.end();  

Now you have to run this file by following command:


nodejs server

This is very basic for mysql using node. you can explore node with the mysql here:

Laravel 5 mysql trigger from migration

Sometimes you need to add trigger in your mysql database of laravel 5.But how to create a trigger using laravel so laravel not provide special function like insertTrigger and something so it problem to create direclty, But laravel DB::unprepared() through we can create trigger for database.

Trigger in Laravel


So here i'm going to to show you how to create migration and create trigger code and how to write drop trigger code, so let's do it this way.


php artisan make:migration create_trigger

Create a Controller for trigger function :


<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTrigger extends Migration
{
    public function up()
    {
        DB::unprepared('CREATE TRIGGER add_employees_salary AFTER INSERT ON `employees` FOR EACH ROW
                BEGIN
                   INSERT INTO `employees_salary` (`employee_id`) VALUES (NEW.id);
                END');
    }
    public function down()
    {
        DB::unprepared('DROP TRIGGER `add_employees_salary`');
    }
}


php artisan migrate

Genreate a pdf using Laravel 5

In this tutorial i will show you how to export pdf or download using maatwebsite package. maatwebsite is a package which is used to genreate pdf file.you have a list of items in tables data you can download it in pdf formate.So lets start how to export or genreate a pdf.

Laravel 5 PDF


Step 1 : Add a new package in your composer.json file.


"maatwebsite/excel": "~2.1.0"

Then, run command composer update.

Step 2 : Now open config/app.php file and add service provider and aliase.


'providers' => [
 'Maatwebsite\Excel\ExcelServiceProvider',
],
'aliases' => [
 'Excel' => 'Maatwebsite\Excel\Facades\Excel',
],

Then run this command and this command is genreate config file for excel package.


php artisan vendor:publish

Step 3 : Also add dompdf package to download the pdf format.


composer require dompdf/dompdf

Step 4 : You have to create some migration command.


php artisan make:migration create_items_table

After this command you will find a file in path database/migrations and add this code to your migration file.


<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemsTable extends Migration
{
   public function up()
   {
       Schema::create('items', function (Blueprint $table) {
           $table->increments('id');
           $table->string('title');
           $table->text('description');
           $table->timestamps();
       });
   }
   public function down()
   {
       Schema::drop("items");
   }
}

After creating the item table now you need to create a model in app/Item.php .


<?php

namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
   public $fillable = ['title','description'];
}

Step 5 :  Create Route and Controller in your app/Http/routes.php file.


Route::get('genreatepdf', 'ExportController@export');

Create a controller in path app/Http/Controllers/ExportController.php and put the below code.


<?php

use Input;
use App\Item;
use Excel;
class ExportController extends Controller
{
 public function export()
 {
    $data = Item::get()->toArray();
    return Excel::create('wantcode', function($excel) use ($data) {
  $excel->sheet('sheet1', function($sheet) use ($data)
     {
   $sheet->fromArray($data);
     });
    })->download("pdf");
 }
}

In this controller you just simply get the items array and pass to the excel function and download as PDF.

How to upload image in Laravel 5

Laravel the amazing framework of php which is most popular now.Today i want to share a post how to upload image in larvael 5.Laravel 5 provide very simple way to upload your image with validation like max size of 2MB, valid file extension like png,jpeg,gif,jpg or svg etc. You can easily implement in your application.

Laravel5


Step 1: You can make a simple route for uploading an image.Then first need to genreate view and second one is for post method.


Route::get('upload-image','ImageController@imageUpload');
Route::post('upload-image','ImageController@imageUploadPost');

Step 2: Now you need to create a controller named ImageController and create a images folder and check the permission of uploading an image. we will store the image in public/images folder.

app/Http/Controllers/ImageController.php


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

class ImageController extends Controller
{

    /**
    * Create view file
    */
    public function imageUpload()
    {
     return view('image-upload');
    }

    /**
    * Manage Post Request
    */
    public function imageUploadPost(Request $request)
    {
     $this->validate($request, [
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $imageName = time().'.'.$request->image->getClientOriginalExtension();
        $request->image->move(public_path('images'), $imageName);

     return back()
      ->with('success','Image Uploaded successfully.')
      ->with('path',$imageName);
    }

}

Step 3: Create a view file for uploading an image.


@if (count($errors) > 0)
<div class="alert alert-danger">
   <strong>Oops!</strong> There were some problems with your input.<br><br>
   <ul>
      @foreach ($errors->all() as $error)
      <li>{{ $error }}</li>
      @endforeach
   </ul>
</div>
@endif
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
   <button type="button" class="close" data-dismiss="alert">×</button>
   <strong>{{ $message }}</strong>
</div>
<img src="/images/{{ Session::get('path') }}">
@endif
<form action="{{ url('image-upload') }}" enctype="multipart/form-data" method="POST">
   {{ csrf_field() }}
   <div class="row">
      <div class="col-md-12">
         <input type="file" name="image" />
      </div>
      <div class="col-md-12">
         <button type="submit" class="btn btn-success">Upload</button>
      </div>
   </div>
</form>

Hope you like the post please comment below for the help.

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...

Popular