简体   繁体   English

Laravel 8 - 如何从数据库中获取数据并将其添加到 VCard 中?

[英]Laravel 8 - How do I fetch data from database and add it in VCard?

I am trying to make vCard for each of my users.我正在尝试为我的每个用户制作 vCard。 I want to fetch data from database to the vCard such as: username, first name, last name, phone number, etc.我想从数据库中获取数据到 vCard,例如:用户名、名字、姓氏、电话号码等。

As I don't have much experience using Laravel, I want to know is it possible to add data to this code:由于我没有太多使用 Laravel 的经验,我想知道是否可以向此代码添加数据:

<?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.我尝试使用{{ $user->email }}, {{ $user->username }}就像我在 Blade.php 文件中使用的一样,但我没有成功。

You first need to take the current user from session.您首先需要从 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM