Friday, December 25, 2015

How to get end user's details in jquery

Get end user's details in jquery:

For getting all the details of any user who is using your website we have to use the following trick:

In this details we can get user's
country name,
city name,
region name,
latitude,
longitude,
ip address,
country code.

For this firstly we have to include jquery library which you can include from here.

Now write this function:



function goToPage() {
    jQuery.ajax({
        url: '//freegeoip.net/json/',
        type: 'POST',
        dataType: 'jsonp',
        success: function (location) {
            var ip=location.ip ;

            var country=location.country_name;
            var country_code= location.country_code;
            var city= location.city ;
            var region=location.region_name;
            var lat= location.latitude;
            var lng=location.longitude;
        }
    });
}



You can use this function where you want.

Thanks

Friday, December 18, 2015

Modify Laravel 5.1 Request Data before validation

Modify Laravel 5.1 request data before validation:

1. Replace All request data
Use replace() function to change all requested data with given data
 For example
$request contains first_name, last_name field.
and we want to create one filed name with combination of first_name and last_name



$newRequest = array('name'=>$request->first_name.$request->last_name);
$request->replace($newRequest);
now $request contains name field.


2. Modify Request field data.
Use merege() function to modify  requested data with given data
For Example
$request contains name field with null value .
and we want to change null to empty string.
$newRequest = array('name' => '');
$request->merge($newRequest);

-Thanks

Friday, December 4, 2015

Problem with laravel save method

Problem with laravel save method:

Laravel save() method save data to database and return inserted data with inserted id . when we save another record after one using same object it  overwrite first record instead of creating new record .
Example

1. create AbcModel.php

public function Abc extend Model{

   public function createNew($data){
       $this->abc_id = $data->id;
        $this->name = $data->name;
     
   }
}
Note : Model Abc only save first record of $users array and in second times it update first record data with second record without creating second record in table.

Solution of above problem is create object of Abc Model inside createNew() method .

public function Abc extend Model{

   public function createNew($data){
    $abc = new Abc();
       $abc->abc_id = $data->id;
        $abc->name = $data->name;
     
   }
}


2. create AbcController.php
class NotificationController extends BaseController {

     public function createAbc (){
       
        $users = array(0 =>array('id' => '1','name'=>'Ravi')
                                  1 => array('id' => '2','name'=>'Ranjan'));

      $new = new Abc();
      foreach($users as $user){
           $new->createNew($user);
        }
     }

}

Friday, November 13, 2015

Multipule table authentication using laravel 5.1

 Multipule table authentication using laravel 5.1:

1. Install laravel 5.1
2.Open terminal and go to laravel instalation folder.
3.Run the following command.
composer require sarav/laravel-mltiauth dev-master
4.Open config/app.php
Replace Illuminate\Auth\AuthServiceProvider:: class with Sarav\Multiauth\MultiauthServiceProvider

5.Modify auth.php
'multi' => [
    'user' => [
        'driver' => 'eloquent',
        'model'  => App\User::class,
        'table'  => 'users'
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model'  => App\Admin::class,
        'table'  => 'admins'
    ]
],
6. Create admins table
7.login using follwing details.
\Auth::attempt("user", ['email' => 'johndoe@gmail.com', 'password' => 'password']);

// Attempts to login admin with email id johndoe@gmail.com
\Auth::attempt("admin", ['email' => 'johndoe@gmail.com', 'password' => 'password']);

Friday, November 6, 2015

Login after admin approval in Laravel 5.1

Login after admin approval in Laravel 5.1:

In Laravel 5.1, here is a system that when we register any user then it's by default logged in. If we want to make a system in which we are in need of admin approval then we have to make it like the following code:

Login after admin approval in Laravel

We have to override two method for this:

1. postLogin
2. postRegister


Open your this file app/Http/Controllers/Auth/AuthController.php

and add these functions.

Login after admin approval in Laravel


    public function postRegister(Request $request)
    {
        $validator = $this->validator($request->all());
        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }
        $this->create($request->all());
        return redirect('/auth/login')->with('status', 'Successfully Registered! You can login after the approval of admin.');
    }



Login after admin approval in Laravel

    public function postLogin(Request $request)
    {
        $this->validate($request, [
            $this->loginUsername() => 'required', 'password' => 'required',
        ]);

        $throttles = $this->isUsingThrottlesLoginsTrait();

        if ($throttles && $this->hasTooManyLoginAttempts($request)) {
            return $this->sendLockoutResponse($request);
        }

        $credentials = $this->getCredentials($request);
        $credentials['status'] = 'active';

        if (Auth::attempt($credentials, $request->has('remember'))) {
            return $this->handleUserWasAuthenticated($request, $throttles);
        }

        if ($throttles) {
            $this->incrementLoginAttempts($request);
        }

        return redirect($this->loginPath())
            ->withInput($request->only($this->loginUsername(), 'remember'))
            ->withErrors([
                $this->loginUsername() => $this->getFailedLoginMessage(),
            ]);
    }


Login after admin approval in Laravel



In postRegister() function there will only registration is possible & in postLogin() we will firstly check its status.


Here in this function $credentials['status'] = 'active' will check if its approved by admin or not.

Here status is my field name in users table and active is its value.

This will work.

Thanks

Friday, October 30, 2015

How to create name based subdomain in apache2 conf.

Create name based subdomain in apache2 conf.:

Some or many time we need to set the same document root for all or almost
each sub domains to a same document root, we can simply do it with apache virtual host  setting as following.

For say if we want that all sub domains of example.com should be hosted on same server and share the same document root then we can use apache web server's ServerAlias Directive as follows

Open this file: /etc/apache2/sites-enabled/000-default.conf

and set this code:


<VirtualHost *:80>
ServerName example.com
ServerAlias server server2.domain.com server2
ServerAlias *.example.com
</VirtualHost>
 

Thanks

Friday, October 23, 2015

How to deploy an ionic app on heroku?

Deploy an ionic app on Heroku:

For deploy your project on heroku as a serving host we can do the following

steps:

1:- At first create a heroku account and install Heroku Toolbelt on your ubuntu system as  directed in following link

https://toolbelt.heroku.com/debian

2. Now initialize a git repository and commit all code to it

as following

git init



3. add heroku remote as following

heroku login

heroku create appname

it gives you back a git and heroku serve url on successful creation.


4. Now go to your terminal and reach to project root you want to deploy on

heroku install any dependencies needed to deploy for example if it a ionic app

as per my case we need to do as follows

npm install express --save

and create the following files

a app.json and write the following code

{

    "name": "app name",

    "description": "App description",

    "repository": "heroku serve url",

    "keywords": ["Keyword1", "Keyword2", "Keyword3"]

}


and a server.js and write the following code to serve node application

var express = require('express'),

    app = express();

app.use(express.static('www'));

app.set('port', process.env.PORT || 5000);

app.listen(app.get('port'), function () {

    console.log('Express server listening on port ' + app.get('port'));

});

and some other dependencies as well should be installed first.

5. Now do the following

git commit -am"comment for version"

git push heroku master

as heroku is the remote name by default created by heroku on app creation

and master is the default git branch.

Got to tour app serve url and check for it.

Thanks