A vCard file, also known as a VCF (Virtual Contact File), is a standard format for sharing contact information. vCards can include: personal information, phone numbers, email addresses, and employment information.
Table Of Content
1 Prerequisites
1.) PHP version of >= 8.2
2.) Composer
3.) Mysql
2 Introduction
Inside this article, we will see how to Export vCard File. So let's follow the below steps and create vcard file in Laravel 11 apps.
3 Create / Install a Laravel Project
3.1 Install Laravel Project
First, ensure Composer is installed on your system. Use the following command to install a new Laravel Project:
Next, create a migration for the posts table using the Laravel artisan command:
php artisan make:migration create_contacts_table
In the migration file located at database/migrations, add the following code to define the contacts table structure:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('contact', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('phone');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('contact');
}
};
Run the migration:
php artisan migrate
5 Create Model
Now, create the Contact model for the posts table. Create the file at app/Models/Contact.php and add the following code:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
protected $fillable = [
'name',
'email',
'phone',
];
}
6 Create Controller (ContactController)
Create a controller to handle the data loading functionality:
php artisan make:controller ContactController
In ContactController.php, create an index and exportvcard function to create vCard:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Contact;
use DataTables;
class ContactController extends Controller
{
public function index(Request $request)
{
$contacts = Contact::get();
return view('contact',compact('contacts'));
}
public function exportvcard(Request $request)
{
$id=$request->id;
$user=Contact::find($id);
// Generate VCF content
$vcfContent = "BEGIN:VCARD\n";
$vcfContent .= "VERSION:3.0\n";
$vcfContent .= "FN:" . $user->name . "\n";
$vcfContent .= "TEL;TYPE=HOME,VOICE:" . $user['phone'] . "\n";
$vcfContent .= "EMAIL:" . $user['email'] . "\n";
$vcfContent .= "END:VCARD\n";
// Define the file name
$fileName = 'contact_' . $id . '.vcf';
// Set the headers to force download
header('Content-Type: text/vcard');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . strlen($vcfContent));
// Output the VCF content
echo $vcfContent;
// Stop further execution (optional, ensures the script doesn't continue)
exit;
}
public function store(Request $request)
{
$jsonStr = file_get_contents('php://input');
$jsonObj = json_decode($jsonStr);
$contact_data = $jsonObj->contact_data;
$contact=new Contact;
$contact->name=$contact_data[0];
$contact->email=$contact_data[1];
$contact->phone=$contact_data[2];
$contact->save();
return response()->json([
'status' => '1',
]);
}
}
?>
7 Create view (Blade)
In the resources/viewsdirectory, create contact.blade.php for the main view: