Friday, 7 October 2022

How to get geo location on registration in laravel?

Get Geo - location details 

 function getLocationInfoByIp(){

    $client  = @$_SERVER['HTTP_CLIENT_IP'];

    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];

    $remote  = @$_SERVER['REMOTE_ADDR'];

    $result  = array('country_name'=>'', 'time_zone'=>'');

    if(filter_var($client, FILTER_VALIDATE_IP)){

        $ip = $client;

    }elseif(filter_var($forward, FILTER_VALIDATE_IP)){

        $ip = $forward;

    }else{

        $ip = $remote;

    }    

    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));    

      

    if($ip_data && $ip_data->geoplugin_countryName != null){

        $result['country_name'] = $ip_data->geoplugin_countryName;

        $result['time_zone'] = $ip_data->geoplugin_timezone;

    }

    return $result;

}

Saturday, 19 March 2022

Restrict number only in input box

 @section('script')

<script type="text/javascript"> 

         function restrictAlphabets(e) {

             var x = e.which || e.keycode;

             if ((x >= 48 && x <= 57))

                 return true;

             else

                 return false;

         }  

</script>

@endsection


onkeypress='return restrictAlphabets(event)'

Thursday, 30 September 2021

How to use multiple database in laravel

 Step 1. In the env file write the code as below.

DB_HOST=127.0.0.1
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=

DB_HOST2=127.0.0.1
DB_DATABASE2=database2
DB_USERNAME2=root
DB_PASSWORD2=

Step 2. In the config/database.php write the code as below.

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'database' => env('DB_DATABASE', 'database1'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
],

'mysql2' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST2', '127.0.0.1'),
    'database' => env('DB_DATABASE2', 'database2'),
    'username' => env('DB_USERNAME2', 'root'),
    'password' => env('DB_PASSWORD2', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
],

Step 3. In the model file add a line as below.
  
         protected $connection = 'mysql2';

Step 4.  

use App\Models\User;

class UsersController extends BaseController
{
   class Order extends Model
   {
      protected $connection = 'mysql2';
      protected $table = 'orders';
  }    
}

 

With Query Builder

$data1 = DB::connection('mysql')->table('users')->get(); // DB 1
$data2 = DB::connection('mysql2')->table('orders')->get(); // DB 2

 

      


Tuesday, 9 February 2021

Guzzle Http Client Request

 composer require guzzlehttp/guzzle

GET REQUEST

public function getGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $request = $client->get('http://example.com');
    $response = $request->getBody();
   
    dd($response);
}


POST REQUEST

public function postGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://example.com/api/posts";
   
    $data['name'] = "codechief";
    $request = $client->post($url,  ['body'=>$data]);
    $response = $request->send();
 
    dd($response);
}


PUT REQUEST

public function putGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://example.com/api/posts/1";
    $data['name'] = "codechief";
    $request = $client->put($url,  ['body'=>$data]);
    $response = $request->send();
   
    dd($response);
}


DELETE REQUEST

public function deleteGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://example.com/api/posts/1";
    $request = $client->delete($url);
    $response = $request->send();
 
    dd($response);
}

How to create and destroy cookie

Set cookie: Cookie::queue(Cookie::make('cookieName', 'value', $minutes));

Get cookie: $value = $request->cookie('cookieName'); or $value = Cookie::get('cookieName');

Forget/remove cookie: Cookie::queue(Cookie::forget('cookieName'));

Check if cookie exist: Cookie::has('cookiename'); or $request->hasCookie('cookiename') will return true or false

Sessions knowledge

 Set session variable in laravel

public function xxxxxx(Request $request, $id)  {
           
    $request->session()->put('key', 'value');

 }

Get session value in laravel

$value = $request->session()->get('key');

Retrieving all data from the session

$data = $request->session()->all();

Checking if An Item Exists In The Session

if ($request->session()->has('users')) {
  //
 }

Destroying session in Laravel

public function showProfile(Request $request, $id) {      
     //Deleting Items From The Session
     $request->session()->forget('key');
     $request->session()->flush();    
 }

Friday, 22 January 2021

How to get data from following and follower in mysql

 

 

Query 1
SELECT
  `connection`.`person_id`,
  `connection`.`follower_id`,
  `people`.`name`,
  `people`.`age`
FROM
  `test`.`people`
  INNER JOIN `test`.`connection`
    ON (
      `people`.`id` = `connection`.`follower_id`
    )
WHERE (`connection`.`person_id` = 3);

Query 2
SELECT person_id, GROUP_CONCAT(DISTINCT follower_id SEPARATOR ', ') AS follower
FROM CONNECTION GROUP BY person_id

Query 3
SELECT person_id, GROUP_CONCAT(DISTINCT follower_id SEPARATOR ', ') AS follower
FROM CONNECTION WHERE (`connection`.`person_id` = 3)
GROUP BY person_id

Query 4
SELECT DISTINCT f.person_id,
o.following,
e.follower
FROM CONNECTION f
LEFT JOIN
(
    SELECT CONNECTION.person_id, GROUP_CONCAT(DISTINCT follower_id SEPARATOR ', ') AS following
    FROM CONNECTION GROUP BY person_id
) AS o ON f.person_id = o.person_id
LEFT JOIN
(
    SELECT CONNECTION.follower_id, GROUP_CONCAT(DISTINCT person_id SEPARATOR ', ') AS follower
    FROM CONNECTION GROUP BY follower_id
) AS e ON f.person_id = e.follower_id

 

Monday, 11 January 2021

Git command

git init
git clone https://github.com/rameshyadavjee/laravel.git
git branch temp
git checkout temp
git add *
git commit –m "Message to go with the commit here"
git config --local user.email rameshyadavjee@gmail.com
git remote add origin https://github.com/rameshyadavjee/laravel.git
git push origin master

 

Run the below code

ssh-keygen
Enter
Enter
Enter

Got to profile and search .ssh   // Example C:\Users\KRISHNA\.ssh

Open the file id_rsa.pub
Copy the content and

https://github.com/settings/keys
Click on New SSH Key
Paste the content

Thats it

How to get last insert id?

 In laravel, when you go with DB query builder then you can use insertGetId() that will insert a record and then return last inserted record id.
Example:
 
        $id = DB::table('users')->insertGetId(
                ['email' => 'rameshyadavjee@gmail.com', 'name' => 'Ramesh']
        );
        print_r($id);
   

If you are inserting a record using Laravel Eloquent then you don't need to use insertGetId() function to get last inserted id. You can simply use create method to insert a new record into. The inserted model instance will be returned to you from the method.
Example:


        $user = User::create(['name'=>'Ramesh','email'=>'rameshyadavjee@gmail.com']);
        print_r($user->id);
 

What is traits?

Traits are a mechanism for code reuse in single inheritance languages such as PHP. Write the same code again, to avoid this use the traits. The traits are used when multiple classes share the same functionality.

Here is an example of trait.

<?php
namespace App\Http\Traits;
use App\Models\Users;

trait UserTrait {
    public function userdata() {
        // Fetch all the user from the 'user' table.
        $user = User::all();
        return $user;
    }
}

************************************************

use App\Http\Traits\UserTrait;

class .....
{
    use UserTrait;
    public function ()
    {
        $data = $this->userdata();
        we will get the data in $data variable.
    }
}


Check if Table/Column Already Exists

Check if Table/Column Already Exists


public function up()
{
  if (!Schema::hasTable('vendor')) {
    Schema::create('vendor', function (Blueprint $table) {
      $table->increments('id');
      $table->string('name');      
      $table->timestamps();
    });
  }
}

public function up()
{
  if (Schema::hasTable('vendor')) {
    Schema::table('vendor', function (Blueprint $table) {
      if (!Schema::hasColumn('vendor', 'address')) {
        $table->string('address');
      }
    });
  }
}

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...