J
J
jouu2019-09-12 17:49:26
Laravel
jouu, 2019-09-12 17:49:26

How to upload data to exel in laravel nova?

HOW TO CORRECTLY TRANSFER THE RESULT OF THE FUNCTION TO THE ARRAY TO UPLOAD IT TO exel.
Good afternoon, forum users, there is a ready-made script that uploads data in Excel format, that is, there is a ready-made resource in which fields and values ​​​​are implemented, and there is also an action that performs the actual recording of values ​​\u200b\u200bin Excel and export.
I need help exporting to excel the value of a function that calculates an employee's work experience in the workplace.
By the way, in the resource on the page, the experience and the period of experience itself are displayed in the field. It's implemented like this and it works
Text::make('Employment', function () {
if ($first = $this->employment_date) {
$second = $this->dissmissal_date ?? Carbon::today();
return ru_year_month_interval($first->diffInMonths($second));
}
return '—';
})->OnlyOnDetail(),
Next, when selecting all employees, I export them to Excel and in the Excel file itself I want to see a column with experience and its values ​​for each employee, I had difficulty with passing the value of the function that displays experience to the array.
SAM CODE : resource

spoiler
<?php

namespace App\Nova;

use Carbon\Carbon;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\DateTime;
//use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Number;
use Illuminate\Http\Request;
use App\Nova\Actions\DownloadExcel;
use App\Traits\NovaSearchesRelationsWithArrays;
use PositionPicker\PositionPicker;

class Position extends Resource
{
    use NovaSearchesRelationsWithArrays;

    public static $searchRelations = [
        'user' => [
            'name',
            'surname',
            ['surname', 'name'],
        ],
    ];

    public static $searchRelationsGlobally = false;

    public static $model = 'App\Position';

    public static $title = 'id';

    public static $search = [
        'id',
    ];

    public static function label()
    {
        return 'Должности';
    }

    public static $with = [
        'user',
        'department',
        'company',
        'leader',
        'positionName',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Text::make('Статус', function () {
                return $this->status;
            }),

            BelongsTo::make('Человек', 'user', 'App\Nova\User')->searchable()->prepopulate(),

            BelongsTo::make('Отдел', 'department', 'App\Nova\Department')
                ->searchable()
                ->prepopulate()
            ,

            PositionPicker::make('Должность', 'positionName', 'App\Nova\PositionName')
                ->nullable(),

            BelongsTo::make('Руководитель', 'leader', 'App\Nova\User')
                ->searchable()
                ->nullable(),

            BelongsTo::make('Компания', 'company', 'App\Nova\Company')
                ->searchable()
                ->prepopulate()
                ->nullable(),

            BelongsTo::make('Вид договора', 'contractType', 'App\Nova\ContractType')
                ->hideFromIndex(),

         Date\Date::make('Дата приёма', 'employment_date')
//                ->format('DD.MM.Y')
                ->nullable()
                ->hideFromIndex(),

            Text::make('Стаж', function () {
                    if ($first = $this->employment_date) {
                        $second = $this->dissmissal_date ?? Carbon::today();
                        return ru_year_month_interval($first->diffInMonths($second));
                    }
                    return '—';
                })->OnlyOnDetail(),

      Date\Date::make('Контрольный срок', 'control_date')
//                ->format('DD.MM.Y')
                ->nullable()
                ->hideFromIndex(),

            Text::make('IP Телефон', 'phone_ip_number')
                ->rules('max:50')
                ->hideFromIndex(),

            Number::make('Срок подбора', 'recruitment_term')
                ->step(1)
                ->hideFromIndex(),

            BelongsTo::make('Канал подбора', 'recruitmentChannel', 'App\Nova\RecruitmentChannel')
                ->searchable()
                ->prepopulate()
                ->nullable()
                ->hideFromIndex(),

            Textarea::make('Комментарий подбора', 'recruitment_channel_comment')
                ->alwaysShow(),

            BelongsTo::make('Рекрутёр', 'recruiter', 'App\Nova\User')
                ->searchable()
                ->nullable()
                ->hideFromIndex(),

           Date\Date::make('Дата увольнения', 'dissmissal_date')
//                ->format('DD.MM.Y')
                ->nullable()
                ->hideFromIndex(),

            Select::make('Причина увольнения', 'dissmissal_reason')
                ->options(dissmissal_reason_map())
                ->displayUsingLabels()
                ->sortable()
                ->hideFromIndex(),

            Textarea::make('Комментарий', 'dissmissal_comment')
                ->alwaysShow(),

            Boolean::make('Экспортировать в Wiki', 'show_on_wiki')
                ->hideFromIndex(),

            DateTime::make('Последнее изменение', 'updated_at')->onlyOnDetail(),

            BelongsTo::make('Последний изменивший', 'updatedBy', 'App\Nova\User')->onlyOnDetail(),
        ];
    }

    public function filters(Request $request)
    {
        return [
            new Filters\DepartmentFilter,
            new Filters\CompanyFilter,
            new Filters\ContractTypeFilter,
            new Filters\PositionCategoryFilter,
            new Filters\DistributionGroupFilter,
            new Filters\WorkExperienceFilter,
            new Filters\PositionStatusFilter,
        ];
    }

    public function lenses(Request $request)
    {
        return [
            new Lenses\PositionControlDates(new \App\Position),
        ];
    }

    public function actions(Request $request)
    {

        dd($request);
     return =
        [new DownloadExcel(
                [
                    'user' => 'Сотрудник',
                    'user_email' => 'Почта',
                    'company' => 'Компания',
                    'department' => 'Отдел',
                    'positionName' => 'Должность',
                    'positionCategory' => 'Категория сотрудника',
                    'contractType' => 'Вид договора',
                    'leader' => 'Руководитель',
                    'control_date' => 'Контрольный срок',
                    'employment_date' => 'Дата приёма',
                    'dissmissal_date' => 'Дата увольнения',
                    'dissmissal_reason' => 'Причина увольнения',
                    'dissmissal_comment' => 'Комментарий',
                    'recruitment_term' => 'Срок подбора',
                    'recruitmentChannel' => 'Канал подбора',
                    'recruitment_channel_comment' => 'Комментарий подбора',
                    'recruiter' => 'Рекрутёр',
                    'distribution_group' => 'Группа рассылки',
                ],
                'positions',
                ['control_date', 'employment_date', 'dissmissal_date'],
                [
                    'user' => 'fio',
                    'user_email' => ['user', 'email'],
                    'company' => 'name',
                    'department' => 'name',
                    'positionName' => 'name',
                    'positionCategory' => ['positionName', 'positionCategoryName'],
                    'distribution_group' => ['positionName', 'distributionGroupName'],
                    'contractType' => 'name',
                    'leader' => 'fio',
                    'recruitmentChannel' => 'name',
                    'recruiter' => 'fio',
                ],
                [
//                    'distribution_group' => distribution_group_map(),
                    'dissmissal_reason' => dissmissal_reason_map(),
                ]


            ),
        ];
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question