Laravel Authentication and Excel Export
1. Install Laravel Fresh New Setup
First, we need to download a fresh Laravel setup. Use the following command:
composer create-project --prefer-dist laravel/laravel Blog
2. Setup Database Credentials
After installing Laravel, configure your database credentials in the .env
file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Then, migrate the tables into the database:
php artisan migrate
3. Define Routes
Create custom routes in routes/web.php
:
Route::get('login', 'AuthController@index');
Route::post('post-login', 'AuthController@postLogin');
Route::get('registration', 'AuthController@registration');
Route::post('post-registration', 'AuthController@postRegistration');
Route::get('dashboard', 'AuthController@dashboard');
Route::get('logout', 'AuthController@logout');
4. Create Authentication Controller
Generate the authentication controller:
php artisan make:controller AuthController
Update app/Http/Controllers/AuthController.php
:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator, Redirect, Response;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Session;
class AuthController extends Controller
{
public function index() {
return view('login');
}
public function registration() {
return view('registration');
}
public function postLogin(Request $request) {
request()->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('dashboard');
}
return Redirect::to("login")->withSuccess('Oops! Invalid credentials');
}
public function postRegistration(Request $request) {
request()->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$this->create($data);
return Redirect::to("dashboard")->withSuccess('Successfully logged in');
}
public function dashboard() {
if(Auth::check()){
return view('dashboard');
}
return Redirect::to("login")->withSuccess('Oops! Access denied');
}
public function create(array $data) {
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
public function logout() {
Session::flush();
Auth::logout();
return Redirect('login');
}
}
5. Create Blade Views
Create login.blade.php
, registration.blade.php
, and dashboard.blade.php
in resources/views/
.
Example login.blade.php
:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form action="{{url('post-login')}}" method="POST">
{{ csrf_field() }}
<input type="email" name="email" placeholder="Email address">
<input type="password" name="password" placeholder="Password">
<button type="submit">Sign In</button>
</form>
</div>
</body>
</html>
6. Export Data to Excel with Laravel Excel
Laravel-Excel is a great package for exporting data. Install it:
composer require maatwebsite/excel
Create an export class:
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class UsersExport implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents
{
public function collection() {
return User::all();
}
public function headings(): array {
return ['#', 'Name', 'Email', 'Created at', 'Updated at'];
}
public function registerEvents(): array {
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:W1';
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
},
];
}
}
Create a controller to handle export:
namespace App\Http\Controllers;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
class ExportController extends Controller
{
public function export() {
return Excel::download(new UsersExport, 'users.xlsx');
}
}
Additional Excel Formatting Options
Wrap text in cells:
$spreadsheet->getActiveSheet()->getStyle('A1:D4')->getAlignment()->setWrapText(true);
Set default font:
$spreadsheet->getDefaultStyle()->getFont()->setName('Arial');
$spreadsheet->getDefaultStyle()->getFont()->setSize(8);
Apply borders:
$styleArray = [
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'FFFF0000'],
],
],
];
$worksheet->getStyle('B2:G8')->applyFromArray($styleArray);
This guide covers authentication and data export, ensuring a well-structured Laravel project.