'Too few arguments to function PendingResourceRegistration::name(),1 passed in C:\xampp\htdocs\project\routes\web.php on line 18 and exactly 2 expected

Too few arguments to function Illuminate\Routing\PendingResourceRegistration::name(), 
1 passed in C:\xampp\htdocs\project\routes\web.php on line 18
and exactly 2 expected.

I Try The Klorofil Template On laravel, but its work perfect. I don't know why but I not open laravel and php for several time. When I open this project again this is hapenning. I just remembered maybe I changed the route or controller because I wanted to use ajax withou refresh, and error, and I forget to change as before. But when is searched again about this template maybe not my fault... or there is update on php.

web.php

Route::get('/', function () {
    return view('main');
});


Route::resource('siswa', 'SiswaController')->name('siswa');
Route::get('/login','AuthController@login')->name('login');
Route::post('/postlogin', 'AuthController@postlogin');
Route::get('/logout','AuthController@logout');


Route::get('/dashboard', 'DashboardController@index')->middleware('auth');
Route::get('siswa.index', 'SiswaController@index')->middleware('auth');
Route::post('siswa.index/import', 'SiswaController@import')->middleware('auth');

controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Siswa;
use DB;
use Excel;

class SiswaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $siswas = siswa::all()->toarray();
        return view('siswa.index', compact('siswas'));

        $siswas = siswa::table('siswas')->orderBy('id', 'DESC')
        ->get();
        return view('siswa.index', compact('siswas'));
    }


    public function import(Request $request)
    {
       $this->validate($request, [
      'select_file'    => 'required|mimes:xls,xlsx'
       ]);

       $path = $request->file('select_file')->getRealPath();

       $siswas = Excel::load($path)->get();

       if ($siswas->count() > 0) 
       {
         foreach ($siswas->toArray() as $key => $value) 
         {
           foreach ($value as $row) 
           {
             $insert_data[] = array(
                    'gambar'         =>  $row['gambar'],
                    'name'           =>  $row['name'],
                    'nisn'           =>  $row['nisn'],
                    'kelas'          =>  $row['kelas'],
                    'jurusan'        =>  $row['jurusan'],
                    'jns_kel'        =>  $row['jns_kel'],
                    'tanggal_lahir'  =>  $row['tanggal_lahir'],
                    'alamat'         =>  $row['alamat']
             );


           }


         }

       if (!empty($insert_data)) 
       {
           siswa::table('siswas')->insert($insert_data);
       }


     }
     return back()->with('success', 'Data Excel Berhasil Di Tambah.');




  }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       return view('siswa.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
              'gambar'    => 'required',
              'name'   => 'required',
              'nisn'   => 'required',
              'kelas'   => 'required',
              'jurusan'   => 'required',
              'jns_kel'   => 'required',
              'tanggal_lahir'   => 'required',
              'alamat'   => 'required'

        ]);
        $siswa = new siswa([
               'gambar'   => $request->get('gambar'),
               'name'  => $request->get('name'),
               'nisn'  => $request->get('nisn'),
               'kelas'  => $request->get('kelas'),
               'jurusan'  => $request->get('jurusan'),
               'jns_kel'  => $request->get('jns_kel'),
               'tanggal_lahir'  => $request->get('tanggal_lahir'),
               'alamat'  => $request->get('alamat')


        ]);
        $siswa->save();
        return redirect()->route('siswa.index')->with('success', 'SUKSES TAMBAH DATA');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $siswa = siswa::find($id);
        return view('siswa.ubah', compact('siswa', 'id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
              'gambar'    => 'required',
              'name'   => 'required',
              'nisn'   => 'required',
              'kelas'   => 'required',
              'jurusan'   => 'required',
              'jns_kel'   => 'required',
              'tanggal_lahir'   => 'required',
              'alamat'   => 'required'        

        ]);
        
        $siswa = siswa::find($id);
        $siswa->gambar = $request->get('gambar');
        $siswa->name = $request->get('name');
        $siswa->nisn = $request->get('nisn');
        $siswa->kelas = $request->get('kelas');
        $siswa->jurusan = $request->get('jurusan');
        $siswa->jns_kel = $request->get('jns_kel');
        $siswa->tanggal_lahir = $request->get('tanggal_lahir');
        $siswa->alamat = $request->get('alamat'); 
        $siswa->save();
        return redirect()->route('siswa.index')->with('success', 'DATA TERBARUI');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $siswa = siswa::find($id);
        $siswa->delete();
        return redirect()->route('siswa.index')->with('success', 'DATA SUDAH DI HAPUS');

    }
}

view folder : siswa.index

@extends('main')
@section('content')
<div class="main">
    <div class="main-content">
        <div class="container-fluid">
           <div class="row">
              <div class="col-md-10" style="margin: 70px 240px" >
                  <div class="panel">
                                <div class="panel-heading">
                                    <h3 class="panel-title">Data Siswa</h3>
                                    <div align="right">
            <!-- Button trigger modal -->
<button type="button" class="btn" data-toggle="modal" data-target="#exampleModal">
  <i class="inr-plus-circle">Tambah Data Siswa</i>
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel" >Tambah Data Siswa</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        
        <form method="POST" action="{{url('siswa')}}">
            {{csrf_field()}}
                <div class="form-group">
                <input type="text" name="gambar" class="form-control" placeholder="Berkas Gambar">
            </div>

                <div class="form-group">
                        <input type="text" name="name" class="form-control" placeholder="Masukkan Nama">
                </div>

                <div class="form-group">
                        <input type="text" name="nisn" class="form-control" placeholder="Masukkan nisn">
                </div>

                <div class="form-group" class="form-control"><p>Masukkan Kelas   :</p>
                <select name="kelas" class="bg-primary text-black">
                <option>X</option>
                <option>XI</option>
                <option>XII</option>
                </select>
                </div>

                <div class="form-group">
                        <input type="text" name="jurusan" class="form-control" placeholder="Jurusan Mu Apa Dong">
                </div>

                <div class="form-group" class="form-control"><p>Jenis Kelamin   :</p>
                <select name="jns_kel" class="bg-primary text-black">
                <option>Cowok</option>
                <option>Cewek</option>
                </select>
                </div>

                <div class="form-group">
                <input type="text" name="tanggal lahir" class="form-control" placeholder="Tanggal Lahir">
                </div>



            <div class="form-group">
                <input type="text" name="alamat" class="form-control" placeholder="Jalan Rumah Mu">
            </div>
            <!-- <div class="form-group">
                <button type="submit" class="btn btn-primary" style="color: black">Kirim</button>
            </div> -->
        
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal" style="background-color: cyan">Close</button>
        <button type="submit" class="btn btn-primary" style="background-color: cyan">Submit</button>
        </form>
      </div>
    </div>
  </div>
</div>
            <br>
            <br>
        </div>
                                </div>
                                <div class="panel-body">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>Gambar</th>
                <th>Name</th>
                <th>Nisn</th>
                <th>Kelas</th>
                <th>Jurusan</th>
                <th>Jenis Kelamin</th>
                <th>Tanggal Lahir</th>
                <th>Alamat</th>
                <th>Ubah</th>
                <th>Hapus</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($siswas as $row)
            <tr>
                <td><img src="<?= $row["gambar"]; ?>" width="50"></td>
                <td>{{$row['name']}}</td>
                <td>{{$row['nisn']}}</td>
                <td>{{$row['kelas']}}</td>
                <td>{{$row['jurusan']}}</td>
                <td>{{$row['jns_kel']}}</td>
                <td>{{$row['tanggal_lahir']}}</td>
                <td>{{$row['alamat']}}</td>
                <td><button class="btn btn-warning"><a href="{{action('SiswaController@edit', $row['id'])}}" style="color: black">UBAH</a></button></td>
                <td>
                    <form method="post" class="delete_form" action="{{action('SiswaController@destroy', $row['id'])}}">
                        {{csrf_field()}}
                        <input type="hidden" name="_method" value="DELETE">
                        <button type="submit" class="btn btn-danger">HAPUS</button>
                    </form>
                </td>
            </tr>
            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
              </div> 
           </div>
        </div>
    </div>
</div>
@stop



@section('content1')
<div class="row">
    <div class="col-md-12">
        <br>
        <h3 align="center">Data Siswa</h3>
        <br>
        @if($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{$message}}</p>
        </div>
        @endif
        <!-- tombol menambah data baru manual -->
        <div align="right">
            <a href="{{route('siswa.create')}}" class="btn btn-primary">Tambah</a>
            <br>
            <br>
        </div>
    <!-- akhir tombol tambah -->
    



    <!-- menambah choose file excel -->
    @if(count($errors) > 0)
    <div class="alert alert-danger">
        Upload Validation Error<br><br>
        <ul>
            @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
    @endif


  


    <form method="post" enctype="multipart/form-data"
    action="{{ url('/siswa.index/import') }}">
    {{ csrf_field() }}
    <div class="form-group">
        <table class="table">
            <tr>
                <td width="40%" align="right"><label>Pilih Untuk Upload File</label></td>
                <td width="30">
                    <input type="file" name="select_file">
                </td>
                <td width="30%" align="left">
                    <input type="submit" name="upload" class="btn btn-primary" value="upload">
                </td>
            </tr>
            <tr>
                <td width="40%" align="right"></td>
                <td width="30"><span class="text-muted">.xls, .xslx</span></td>
                <td width="30%" align="left"></td>
            </tr>
        </table>
    </div>
        








    </form>
    <!-- akhir menambah choose file excel -->
    


    
        <div class="table-responsive">
        <table class="table table-bordered">
            <tr>
                <th>Gambar</th>
                <th>Name</th>
                <th>Nisn</th>
                <th>Kelas</th>
                <th>Jurusan</th>
                <th>Jenis Kelamin</th>
                <th>Tanggal Lahir</th>
                <th>Alamat</th>
                <th>Ubah</th>
                <th>Hapus</th>
            </tr>
            @foreach($siswas as $row)
            <tr>
                <td><img src="<?= $row["gambar"]; ?>" width="50"></td>
                <td>{{$row['name']}}</td>
                <td>{{$row['nisn']}}</td>
                <td>{{$row['kelas']}}</td>
                <td>{{$row['jurusan']}}</td>
                <td>{{$row['jns_kel']}}</td>
                <td>{{$row['tanggal_lahir']}}</td>
                <td>{{$row['alamat']}}</td>
                <td><button class="btn btn-warning"><a href="{{action('SiswaController@edit', $row['id'])}}" style="color: black">UBAH</a></button></td>
                <td>
                    <form method="post" class="delete_form" action="{{action('SiswaController@destroy', $row['id'])}}">
                        {{csrf_field()}}
                        <input type="hidden" name="_method" value="DELETE">
                        <button type="submit" class="btn btn-danger">HAPUS</button>
                    </form>
                </td>
            </tr>
            @endforeach
        </table>
    </div>
    </div>
</div>


    
@endsection

view folder : main.blade.php

<!doctype html>
<html lang="en">

<head>
    <title>Dashboard | Klorofil - Free Bootstrap Dashboard Template</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <!-- VENDOR CSS -->
    <link rel="stylesheet" href="{{asset('admin/assets/vendor/bootstrap/css/bootstrap.min.css')}}">
    <link rel="stylesheet" href="{{asset('admin/assets/vendor/font-awesome/css/font-awesome.min.css')}}">
    <link rel="stylesheet" href="{{asset('admin/assets/vendor/linearicons/style.css')}}">
    <link rel="stylesheet" href="{{asset('admin/assets/vendor/chartist/css/chartist-custom.css')}}">
    <!-- MAIN CSS -->
    <link rel="stylesheet" href="{{asset('admin/assets/css/main.css')}}">
    <!-- FOR DEMO PURPOSES ONLY. You should remove this in your project -->
    <link rel="stylesheet" href="{{asset('admin/assets/css/demo.css')}}">
    <!-- GOOGLE FONTS -->
    <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700" rel="stylesheet">
    <!-- ICONS -->
    <link rel="apple-touch-icon" sizes="76x76" href="{{asset('admin/assets/img/apple-icon.png')}}">
    <link rel="icon" type="image/png" sizes="96x96" href="{{asset('admin/assets/img/favicon.png')}}">
</head>

<body>
    <!-- WRAPPER -->
    <div id="wrapper">
        <!-- NAVBAR -->
        <nav class="navbar navbar-default navbar-fixed-top">
            <div class="brand">
                <a href="index.html"><img src="{{asset('admin/assets/img/logo-dark.png')}}"  alt="Klorofil Logo" class="img-responsive logo"></a>
            </div>
            <div class="container-fluid">
                <div class="navbar-btn">
                    <button type="button" class="btn-toggle-fullwidth"><i class="lnr lnr-arrow-left-circle"></i></button>
                </div>
                <form class="navbar-form navbar-left">
                    <div class="input-group">
                        <input type="text" value="" class="form-control" placeholder="Search Data Siswa...">
                        <span class="input-group-btn"><button type="button" class="btn btn-primary">Go</button></span>
                    </div>
                </form>
                
                <div id="navbar-menu">
                    <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle icon-menu" data-toggle="dropdown">
                                <i class="lnr lnr-alarm"></i>
                                <span class="badge bg-danger">5</span>
                            </a>
                            <ul class="dropdown-menu notifications">
                                <li><a href="#" class="notification-item"><span class="dot bg-warning"></span>System space is almost full</a></li>
                                <li><a href="#" class="notification-item"><span class="dot bg-danger"></span>You have 9 unfinished tasks</a></li>
                                <li><a href="#" class="notification-item"><span class="dot bg-success"></span>Monthly report is available</a></li>
                                <li><a href="#" class="notification-item"><span class="dot bg-warning"></span>Weekly meeting in 1 hour</a></li>
                                <li><a href="#" class="notification-item"><span class="dot bg-success"></span>Your request has been approved</a></li>
                                <li><a href="#" class="more">See all notifications</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="lnr lnr-question-circle"></i> <span>Help</span> <i class="icon-submenu lnr lnr-chevron-down"></i></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Basic Use</a></li>
                                <li><a href="#">Working With Data</a></li>
                                <li><a href="#">Security</a></li>
                                <li><a href="#">Troubleshooting</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="{{asset('admin/baik.jpg')}}" class="img-circle" alt="Avatar"> <span>{{auth()->user()->name}}</span> <i class="icon-submenu lnr lnr-chevron-down"></i></a>
                            <ul class="dropdown-menu">
                                <li><a href="#"><i class="lnr lnr-user"></i> <span>My Profile</span></a></li>
                                <li><a href="#"><i class="lnr lnr-envelope"></i> <span>Message</span></a></li>
                                <li><a href="#"><i class="lnr lnr-cog"></i> <span>Settings</span></a></li>
                                <li><a href="/logout"><i class="lnr lnr-exit"></i> <span>Logout</span></a></li>
                            </ul>
                        </li>
                        <!-- <li>
                            <a class="update-pro" href="https://www.themeineed.com/downloads/klorofil-pro-bootstrap-admin-dashboard-template/?utm_source=klorofil&utm_medium=template&utm_campaign=KlorofilPro" title="Upgrade to Pro" target="_blank"><i class="fa fa-rocket"></i> <span>UPGRADE TO PRO</span></a>
                        </li> -->
                    </ul>
                </div>
            </div>
        </nav>
        <!-- END NAVBAR -->
        <!-- LEFT SIDEBAR -->
        <div id="sidebar-nav" class="sidebar">
            <div class="sidebar-scroll">
                <nav>
                    <ul class="nav">
                        <li><a href="/" class=""><i class="lnr lnr-home"></i> <span>Dashboard</span></a></li>
                        <li><a href="siswa" class="active"><i class="lnr lnr-user"></i> <span>Siswa</span></a></li>
                        <!-- <li><a href="charts.html" class=""><i class="lnr lnr-chart-bars"></i> <span>Charts</span></a></li>
                        <li><a href="panels.html" class=""><i class="lnr lnr-cog"></i> <span>Panels</span></a></li>
                        <li><a href="notifications.html" class=""><i class="lnr lnr-alarm"></i> <span>Notifications</span></a></li>
                        <li>
                            <a href="#subPages" data-toggle="collapse" class="collapsed"><i class="lnr lnr-file-empty"></i> <span>Pages</span> <i class="icon-submenu lnr lnr-chevron-left"></i></a>
                            <div id="subPages" class="collapse ">
                                <ul class="nav">
                                    <li><a href="page-profile.html" class="">Profile</a></li>
                                    <li><a href="page-login.html" class="">Login</a></li>
                                    <li><a href="page-lockscreen.html" class="">Lockscreen</a></li>
                                </ul> -->
                            </div>
                        </li>
                        <!-- <li><a href="tables.html" class=""><i class="lnr lnr-dice"></i> <span>Tables</span></a></li>
                        <li><a href="typography.html" class=""><i class="lnr lnr-text-format"></i> <span>Typography</span></a></li>
                        <li><a href="icons.html" class=""><i class="lnr lnr-linearicons"></i> <span>Icons</span></a></li> -->
                    </ul>
                </nav>
            </div>
        </div>
        <!-- END LEFT SIDEBAR -->
        <!-- MAIN -->
        @yield('content')
        <!-- END MAIN -->
        <div class="clearfix"></div>
        <footer>
            <div class="container-fluid">
                <p class="copyright">Shared by <i class="fa fa-love"></i><a href="https://bootstrapthemes.co">BootstrapThemes</a>
</p>
            </div>
        </footer>
    </div>
    <!-- END WRAPPER -->
    <!-- Javascript -->
    <script src="http://code.jquery.com/jquery-3.4.0.js" integrity="sha256-DYZMCC8HTC+QDr5QNaIcfR7VSPtcISykd+6eSmBW5qo=" crossorigin="anonymous"></script>
    <script src="{{asset('admin/assets/vendor/jquery/jquery.min.js')}}"></script>
    <script src="{{asset('admin/assets/vendor/bootstrap/js/bootstrap.min.js')}}"></script>
    <script src="{{asset('admin/assets/vendor/jquery-slimscroll/jquery.slimscroll.min.js')}}"></script>
    <script src="{{asset('admin/assets/vendor/jquery.easy-pie-chart/jquery.easypiechart.min.js')}}"></script>
    <script src="{{asset('admin/assets/vendor/chartist/js/chartist.min.js')}}"></script>
    <script src="{{asset('admin/assets/scripts/klorofil-common.js')}}"></script>

    

</body>

</html>

the result must give me a table some person data



Solution 1:[1]

The solution is to change your route to:

Route::resource('siswa', 'SiswaController')->name('*', 'siswa')

This may come very late but I've just encountered the same error. The error occurs because when we use Route::resource, Laravel expects 2 parameters instead of just 1. Example, in your:

Route::resource('siswa', 'SiswaController')->name('siswa')

you only defined name. Hence, Laravel has defined the route 'siswa' for you.

But because you are using Route::resource, there are many routes that are defined in there, such as create, edit, delete and so on. So Laravel needs to define the routes as 'siswa.create', 'siswa.delete', and so on. Naturally, we cannot be expected to list out all of the methods, so putting a * helps Laravel understand that it should be creating the named routes as siswa.* and the * represents all of the different methods(e.g. create,edit, delete and so on). Hope this helped!

Solution 2:[2]

The route:

Route::get('siswa.index', 'SiswaController@index')->middleware('auth');

is causing all of your problems. You declared a route resource up top and then you declared another route named siswa.index which was already there because of the line Route::resource('siswa', 'SiswaController')->name('siswa');. Creating a Resource makes routes as follows:

Route::get('siswa', 'SiswaController@index')->name('siswa.index);
Route::post('siswa', 'SiswaController@store')->name('siswa.store);
Route::get('siswa/create', 'SiswaController@create')->name('siswa.create);
Route::post('siswa/{siswa}', 'SiswaController@update')->name('siswa.update);
Route::get('siswa/{siswa}', 'SiswaController@show')->name('siswa.show);
Route::delete('siswa/{siswa}', 'SiswaController@destroy')->name('siswa.destroy);
Route::get('siswa/{siswa}/edit', 'SiswaController@edit')->name('siswa.edit);

So if a route name is declared twice, it will throw a missing argument error.

Solution 3:[3]

Simple remove ->name('siswa') from web.php on line 18.

Route::resource('siswa', 'SiswaController');

And use route name to blade file i.g : get index page {{ route('siswa.index') }}, create form {{ route('siswa.create') }}

Method          URI                         Route name              Action
GET|HEAD        siswa                       siswa.index             App\Http\Controllers\SiswaController@index
POST            siswa                       siswa.store             App\Http\Controllers\SiswaController@store
GET|HEAD        siswa/create                siswa.create            App\Http\Controllers\SiswaController@create
PUT|PATCH       siswa/{amenity}             siswa.update            App\Http\Controllers\SiswaController@udpate
GET|HEAD        siswa/{amenity}             siswa.show              App\Http\Controllers\SiswaController@show
DELETE          siswa/{amenity}             siswa.destroy           App\Http\Controllers\SiswaController@destroy
GET|HEAD        siswa/{amenity}/edit        siswa.edit              App\Http\Controllers\SiswaController@edit

Solution 4:[4]

I have got a same today. I found a result When I Used route:list, I got result

GET HEAD admin/user user.index App\Http\Controllers\AdminUserController@index web
GET HEAD admin/user/create user.create App\Http\Controllers\AdminUserController@create

I Have changed my route: as follows

Route::resource('/admin/user', 'AdminUserController' , ['as' => 'admin'] );

Then

I got a result on Route:list

GET HEAD admin/user admin.user.index App\Http\Controllers\AdminUserController@index web
GET HEAD admin/user/create admin.user.create App\Http\Controllers\AdminUserController@create

Therefore I can suggest to change your route file as follows:

Route::resource('/admin/user', 'AdminUserController' , ['as' => 'admin'] );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 rkg
Solution 3
Solution 4