Convert English date to Nepali date(AD-BS) in laravel
Install Laravel Project
Run the command to induct fresh Laravel project.
composer create-project laravel/laravel nepali-date --prefer-dist
Setup Database
Go to your project .env file and set up database credential and move
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name
DB_USERNAME=here database username
DB_PASSWORD=here database password
Create Migration
Now you will create a user table then follow this command in your terminal:
php artisan make:model User -m
Now In the Migration file,
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration{public function up(){Schema::create('users', function (Blueprint $table) {$table->id();$table->string('name');$table->string('email')->unique();$table->timestamp('email_verified_at')->nullable();$table->string('password');$table->rememberToken();$table->timestamps();});}
Next,this command use to in your terminal then this setup create to in your database.
php artisan migrate
The complete code in User Model is given below.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory;
protected $fillable = ['name','email','password',];}
Now let’s makes our required routes in routes/web.php
Route::get(‘/dashboard’, ‘App\Http\Controllers\DashboardController@index’)->name(‘dashboard’);
Now Let’s create our DashboardController by running the following command.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;class DashboardController extends Controller
{public function index()
{return view('userdash');}}
Once you have created a database,model,route,controller, you may install shankhadev/bsdate :
just add the following to your composer.json in require. Then run composer update:
"shankhadev/bsdate": "master-dev"
Open your config/app.php
and add the following to the providers
array:
Shankhadev\Bsdate\BsdateServiceProvider::class,
In the same config/app.php
and add the following to the aliases
array:
'Bsdate' => Shankhadev\Bsdate\BsdateFacade::class,
Now let’s see the complete code for our View page — userdash.blade.php file
<x-app-layout><x-slot name="header"><h2 class="font-semibold text-xl text-gray-800 leading-tight">{{ __('Dashboard for user') }}</h2></x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8"><div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"><div class="p-6 bg-white border-b border-gray-200">You're logged in as a user!</div></div></div></div><div class="py-12"><div class="max-w-7xl mx-auto sm:px-6 lg:px-8"><div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"><div class="p-6 bg-white border-b border-gray-200"><p>Name: {{auth()->user()->name}} </p><p>Email: {{auth()->user()->email}} </p><p>CreatedAt :@php$year= auth()->user()->created_at->format('Y');$month= auth()->user()->created_at->format('m');$day= auth()->user()->created_at->format('d');$date=Bsdate::eng_to_nep($year,$month,$day);echo $date['date'].' '.$date['nmonth'].' '.$date['year'].','.$date['day']@endphp</p>{{-- OR --}}{{-- @php$year= auth()->user()->created_at->format('Y');$month= auth()->user()->created_at->format('m');$day= auth()->user()->created_at->format('d');$date=Bsdate::eng_to_nep($year,$month,$day);@endphp<p>CreatedAt :{{$date['date'].' '.$date['nmonth'].' '.$date['year'].','.$date['day']}} </p> --}}</div>
</div>
</div>
</div>
</x-app-layout>