【问题标题】:Laravel check if user have user name if not show user id excelLaravel检查用户是否有用户名,如果不显示用户ID excel
【发布时间】:2020-11-17 11:55:46
【问题描述】:

我正在使用 laravel-excel 插件。

我正在用 excel 构建映射。如果用户名不存在,我想只显示user_id,如果用户按下下载excel文件。我尝试了一些方法,但没有结果。

    public function map($user): array
{
    return[
        $user->id,
        $user->user->name != $user->user_id,
        $user->organization->name,
        $user->course_id,
        $user->start_date,
        $user->end_date,
        $user->completed,
        $user->score,
    ];

}

【问题讨论】:

    标签: laravel


    【解决方案1】:

    使用三元演说

    public function map($user): array
    {
        return [
            $user->id,
            isset($user->user->name) && !empty($user->user->name)  ? $user->user->name : $user->user_id,
            $user->organization->name,
            $user->course_id,
            $user->start_date,
            $user->end_date,
            $user->completed,
            $user->score,
        ];
    }
    

    【讨论】:

    • @RonvanderHeiden isset 还可以检查 key 是否存在,它在 nosql 数据库中很方便,在 nosql dbs 的情况下只使用空会破坏代码,尽管在 sql dbs 中只使用空是可以的
    猜你喜欢
    • 2017-11-10
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多