Monday, 2 July 2018

How to export data in csv / xls/ xlsx?

Step1:

Execute this command "composer require maatwebsite/excel"

Add Service Provider and Facade on config/app.php.
      Maatwebsite\Excel\ExcelServiceProvider::class,

     'Excel' => Maatwebsite\Excel\Facades\Excel::class,

execute this command :
php artisan vendor:publish --provider=Maatwebsite\Excel\ExcelServiceProvider

Step2:
Create a controller for DataTables application using Artisan.
php artisan make:controller DatatablesController
namespace App\Http\Controllers;
use App\Http\Requests;
use App\User;
use Yajra\Datatables\Datatables;

class DatatablesController extends Controller
{
    public function getIndex()
    {
        return view('datatables.index');
    }  

    public function anyData()
    {
        return Datatables::of(User::query())->make(true);
    }
}


Step:3
Register export routes in app\Htpp\routes.php
Route::get('datatables/{type}', 'DatatablesController@export_users');


***********************************************************
Step:4

Create a view file and add the below code.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel DataTables Tutorial</title>
<!-- Bootstrap CSS -->
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
<style>
body {
    padding-top: 40px;
}
</style>
<!-- jQuery -->
<script src="//code.jquery.com/jquery.js"></script>
<!-- DataTables -->
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<!-- Bootstrap JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<!-- App scripts -->
</head>
<body>
<div class="container">
  <div class="pull-right"> <a class="btn btn-primary" href="{{ url('users/csv') }}">Export to Csv </a> </div>
  <table class="table table-bordered" id="users-table">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Created At</th>
        <th>Updated At</th>
      </tr>
    </thead>
  </table>
</div>
<script>
$(function() {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: "{!! route('datatables.data') !!}",
        columns: [
            { data: 'id', name: 'id' },
            { data: 'name', name: 'name' },
            { data: 'email', name: 'email' },
            { data: 'created_at', name: 'created_at' },
            { data: 'updated_at', name: 'updated_at' }
        ]
    });
});
</script>
</body>
</html>

Sunday, 10 December 2017

How to remove public from url.


Copy .htaccess from public folder and paste to root directory.

Rename server.php to index.php update below lines.

How to remove error when first time php artisan migrate is done.


Step1: composer create-project --prefer-dist laravel/laravel project-name

Step2: cd project-name

Step3: composer require laravel/ui

Step4: php artisan ui bootstrap --auth

Step5: npm install && npm run development

Step6: open the .en file write the database name, username and password as below.
DB_DATABASE=demo2
DB_USERNAME=root
DB_PASSWORD=

Step7: Add below line in app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Schema;
and replace boot() with below lines

 public function boot()
    {
        Schema::defaultStringlength(100);
    }

*******************************************************
open below files from /database/migrations
create_users_table.php and create_password_resets_table.php

If you want to change the default email length change and update as below.

The migration of password_resets's up() method would look like this:

Schema::create('password_resets', function (Blueprint $table) {
    $table->string('email');
    $table->string('token');
    $table->timestamp('created_at')->nullable();

    $table->unique([DB::raw('email(50)')]);
});

And for the migration of users:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email');
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();

    $table->index([DB::raw('email(50)')]);
});

Step7: run php artisan migrate

Step8: To to remove public from url.

Friday, 8 September 2017

2 ways to logout in laravel

There are 2 ways to logout from laravel.


1st way
Routing.
    Route::get('/logout', 'Auth\LoginController@logout');

 Blade file.
     <?php if(Auth::check()){ ?>
    <a href="{{ url('/logout') }}">Logout</a>
    <?php } ?>

2nd way
Routing.
      Route::post('logout','auth\LoginController@getLogout');

Controller.
     public function getLogout()
    {
        $this->auth->logout();
        return redirect('/login');
    }


Blade file.
 <?php if(Auth::check()){ ?>
<a href="{{ url('/logout') }}" class="btn-primary" style="padding:10px;" onclick="event.preventDefault();document.getElementById('logout-form').submit();"> Logout </a>
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}</form>

   <?php } ?>

Sunday, 9 July 2017

How to create yajra datatable?

Step1:
Execute this command "composer require yajra/laravel-datatables-oracle"
Add Datatables Service Provider and Facade on config/app.php.

        Yajra\Datatables\DatatablesServiceProvider::class,

       'Datatables' => Yajra\Datatables\Facades\Datatables::class,

Step2:
Create a controller for DataTables application using Artisan.
php artisan make:controller DatatablesController

namespace App\Http\Controllers;

use App\Http\Requests;
use App\User;
use Yajra\Datatables\Datatables;

class DatatablesController extends Controller
{
    public function getIndex()
    {
        return view('datatables.index');
    }
   
    public function anyData()
    {
        return Datatables::of(User::query())->make(true);
    }
}

Step:3
Register datatables routes in app\Htpp\routes.php

Route::get('/datatables', ['uses'=>'DatatablesController@getIndex']);
Route::get('/datatables/datatable', ['as'=>'datatables.data','uses'=>'DatatablesController@anyData']);

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

Step:4
Create a view file and add the below code.
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel DataTables Tutorial</title>

        <!-- Bootstrap CSS -->
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
        <style>
            body {
                padding-top: 40px;
            }
        </style>
    </head>
    <body>
        <div class="container">
           <table class="table table-bordered" id="users-table">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Email</th>
                <th>Created At</th>
                <th>Updated At</th>
            </tr>
        </thead>
    </table>
        </div>

<script>
$(function() {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{!! route('datatables.data') !!}',
        columns: [
            { data: 'id', name: 'id' },
            { data: 'name', name: 'name' },
            { data: 'email', name: 'email' },
            { data: 'created_at', name: 'created_at' },
            { data: 'updated_at', name: 'updated_at' }
        ]
    });
});
</script>
        <!-- jQuery -->
        <script src="//code.jquery.com/jquery.js"></script>
        <!-- DataTables -->
        <script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
        <!-- Bootstrap JavaScript -->
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
        <!-- App scripts -->
        @stack('scripts')
    </body>
</html>

How to create virtual host for in xampp

Step1:
In your C:\xampp\apache\conf\extra. Open httpd-vhosts.conf and below lines.

<VirtualHost jayesh.shah:80> 
    DocumentRoot "D:/xampp/htdocs/jayesh/public"
    ServerName jayesh.shah
ServerAlias www.jayesh.shah
<Directory "D:/xampp/htdocs/jayesh/public">
Options +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Step2:
In your C:\Windows\System32\drivers\etc\
Open hosts file and add below line
                  192.168.10.106 jayesh.shah
                  192.168.10.106 www.jayesh.shah

Step3: Restart apache

Wednesday, 26 April 2017

How to create helper in laravel?

Basically there are there steps.

Step 1:    Within your app/Libraries directory, create a Helpers.php file and add your functions.
Step 2:    Within composer.json, in the autoload block, add "files": ["app/Libraries/helpers.php"].
Step 3:    Run composer update.
Step 4:    Showing the content. 
Helpers.php
<?php
    function notification_count()
    {
        $user_loggedin = Auth::user();
        $id = $user_loggedin->id;  
        $sql =  DB::table('notification')
                ->where('read_status', 0)
                ->where('user_id',$id)
                ->count('read_status');
    echo $sql;                            
    } 
?>

composer.json

"files": [
        "app/Libraries/Helpers.php"
        ],   
       
Showing the content
 <span class="badge" style="color:#FFFFFF !important;">{{ notification_count() }}</span> <i class="fa fa-caret-down"></i></a>

CRUD

Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
use Session;

class Flight extends Model {
    protected $table = 'flight';
    protected $primaryKey = 'id';
    // protected $guarded = [];
    // protected $hidden = ['id'];
    protected $fillable = ['name','age','status'];
    public $timestamps = true;


    public function updateflight ($id,$data)
    {
      $updatedRows = App\Flight::where('id', $id)->update('name'=>$data['name'],'status'=>$data['status'])->save();
      return  $updatedRows;   
    } 

    public function deleteflight ($id)
    {
      $deletedRows = App\Flight::where('id', $id)->where('status','0')->delete();
      return  $deletedRows;   
    }
}


?>
Controller

<?php
namespace App\Http\Controllers;
use App\Models\Flight;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

class FlightController extends Controller
{  
    public function addrecord(Request $request)
    {
    
       $postData = Input::all();
     
        $messages = array(
             'name.required'=>'You cant leave name field empty',
             'age.required'=>'You cant leave age field empty',
             'status.required'=>'Age cannot be blank',
    );

    $rules = array(
        'name'=>'required | min:5',',
        'age'=>''required ',
        'status'=>'required',
    );
   
     $validator = Validator::make($postData, $rules,$messsages);   
     
  if($validator->fails()) {                         
                 return Redirect::to('sales/createnew')->withInput()->withErrors($validator);           
     }else {
       
      $flight = new Flight;
      $flight->name = $request->name;
      $flight->age = $request->age;
      $flight->status = $request->status;
     
      $flight->save();
      return redirect('dashboard')->with('status', 'Sucessfully inserted!');  
      }
}
   
    public function updaterecord(Request $request)
    {
      // Validate the request...
      $id = $request->id;
      $flight = new Flight;
     
      $flight_array = Flight::find( $id );

    //CHECK FOR NEW OR EXISTING Flight
        if( $flight_array ) {
            $flight_array->name = $request->name;
            $flight_array->age = $request->age;
            $flight_array->status = $request->status;
            $flight_array->save();
        }   
    or
   
        $data['name'] = $request->name;
        $data['age'] = $request->age;     
        $data['status'] = $request->status;
   
        $updated = $flight->updateflight($id,$data);
        if($updated  > 0 )
        {          
           Session::flash('success',"Sucessfully updated!");
           return redirect('dashboard');  
           or
           return redirect('dashboard')->with('success', 'Sucessfully updated!');  
        } else {
       
           return redirect('dashboard')->with('error', 'Try again!');  
        }       
       
     }
       
    public function deleterecord(Request $request)
    {
      // Validate the request...
      $id = $request->id;
      $flight = new Flight;     
      $flight_array = Flight::find( $id );

    //CHECK FOR NEW OR EXISTING Flight
    if( $flight_array )
      {
        $flight_array->delete();
      }
   
    Or
        $deletedRows = $flight->deleteflight($id);
        if($deletedRows  > 0 )
        {          
           return redirect('dashboard')->with('status', 'Sucessfully deleted!');  
        } else {
           return redirect('dashboard')->with('error', 'Try again!');  
        }
    }
       
}

?>

**************************************
View file
<form action="" method="post" name="form1">
  <table width="200" border="1">
    <tr>
      <td colspan="2">
@if(Session::has('success'))
<div class="alert alert-success">{{ Session::get('success') }}</div>
@endif

Error on view page for all the errors.
@foreach ($errors->all() as $error)
   <p class="alert alert-danger">{{ $error }}</p>
@endforeach

</td>
     
    </tr>
    <tr>
      <td>Name</td>
      <td>

     <input type="text" class="form-control" id="name" placeholder="Name" name="name" value="{{ old('name') }}">
        <?php if($errors->first('name') ){ ?><div class="alert alert-danger"> {{ $errors->first(name') }}</div><?php }  ?>

      </td>
    </tr>
    <tr>
      <td>Age</td>
      <td>
  <input type="text" class="form-control" id="age" placeholder="Age" name="age" value="{{ old('age') }}">
       <?php if($errors->first('age') ){ ?><div class="alert alert-danger"> {{ $errors->first(age') }}</div><?php }  ?>

      </td>
    </tr>
    <tr>
      <td>Status</td>
      <td><input name="status" type="radio" value="Active">
        <input name="status" type="radio" value="Inactive">

        </td>
    </tr>
  </table>
</form>

Monday, 24 April 2017

How to create active menu selection?


<div class="collapse navbar-collapse">
      <ul class="nav navbar-nav navbar-right">
        <li class="{{ (Request::is('index') ? 'active' : '') }}"><a href="index">Home</a></li>
        <li class="{{ Request::is('about') ? 'active' : '' }}"><a href="about-us">About Us</a></li>
        <li class="{{ Request::is( 'services') ? 'active' : '' }}"><a href="services">Services</a></li>
        <li class="{{ Request::is( 'portfolio') ? 'active' : '' }}"><a href="portfolio">Portfolio</a></li>
        <li class="dropdown {{ Request::is( 'privacy-policy') ? 'active' : '' | Request::is( 'terms-of-use') ? 'active' : '' }}"> <a href="javascript::" class="dropdown-toggle" data-toggle="dropdown">Pages <i class="icon-angle-down"></i></a>
          <ul class="dropdown-menu">
            <li><a href="#">Dropdown Menu 1</a></li>
            <li class="divider"></li>
            <li><a href="privacy-policy">Privacy Policy</a></li>
            <li><a href="terms-of-use">Terms of Use</a></li>
          </ul>
        </li>
        <li><a href="blog">Blog</a></li>
        <li class="{{ Request::is( 'contact-us') ? 'active' : '' }}"><a href="contact-us">Contact</a></li>
      </ul>
    </div>

How to comment in laravel?



It is very necessary to comment in laravel format, not in html or php.

Syntax: {{--    --}}

It will not shown the comment in our page if you see the source code.

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...