Home > Software design >  Laravel 8 - How do I fetch data from database and add it in VCard?
Laravel 8 - How do I fetch data from database and add it in VCard?

Time:07-28

I am trying to make vCard for each of my users. I want to fetch data from database to the vCard such as: username, first name, last name, phone number, etc.

As I don't have much experience using Laravel, I want to know is it possible to add data to this code:

<?php

    use JeroenDesloovere\VCard\VCard;


        // define vcard
        $vcard = new VCard();

        // define variables
        $lastname = 'firstname';
        $firstname = 'lastname';
        $additional = '';
        $prefix = '';
        $suffix = '';

        // add personal data
        $vcard->addName($lastname, $firstname, $additional, $prefix, $suffix);

        // add work data
        $vcard->addCompany('');
        $vcard->addJobtitle('');
        //$vcard->addRole('');
        $vcard->addEmail('');
        $vcard->addPhoneNumber(123451231, 'PREF;WORK');

        // return vcard as a string
        //return $vcard->getOutput();

        // return vcard as a download
        return $vcard->download();

        // save vcard on disk
        //$vcard->setSavePath('/path/to/directory');
        //$vcard->save();

I tried using {{ $user->email }}, {{ $user->username }} like I use in blade.php files but I wasn't successful with it.

CodePudding user response:

You first need to take the current user from session. That can be made like that

$user = \Auth::user();

After you need to assign all the values to your variables like that

$lastname = $user->firstname;
$firstname = $user->lastname;
  • Related