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);
        }
     }

}