Laravel Проблема с импортом - PullRequest
0 голосов
/ 16 февраля 2020

У меня есть эта проблема, когда я импортирую таблицу Excel в Laravel, я пытаюсь мгновенно создать таблицу, когда импортирую ее и динамически создаю поля:

Symfony \ Component \ Синтаксическая ошибка Debug \ Exception \ FatalThrowableError, неожиданное 'if' (T_IF), ожидаемое ']'

- это модель импорта для импорта файлов

 <?php

 namespace App\Imports;

 use DB;
 use App\People;
 use Maatwebsite\Excel\Concerns\ToModel;
 use Illuminate\Support\Facades\Schema;
 use Illuminate\Database\Schema\Blueprint;

 class PeopleImport implements ToModel
 {
        // importing excel sheet or creating new schema

        public function model(array $row)
        {
          return new People([
             if (!Schema::hasTable($table_name))
                {
                 Schema::connection('mysql')->create($table_name, function (Blueprint $table) use 
                 ($ff, $table_name) {
                    if (count($ff) > 0) {
                           foreach ($ff as $field) {
                            $table->longText($field)->nullable();
                            $array[] = $field;
                          }
                      }
                });

                foreach($data->toArray() as $row2) {
                     $insert = DB::table($table_name)->insert($row2);

                }
            }
        ]);
        }
   }

и это контроллер для импорта файлов

   <?php

    namespace App\Http\Controllers;

    use App\People;
    use DB;
    use Illuminate\Http\Request;
    use App\Exports\PeopleExport;
    use App\Imports\PeopleImport;
    use Maatwebsite\Excel\Facades\Excel;
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;


     class PeopleController extends Controller
       {
       /**
       * @return \Illuminate\Support\Collection
       */
       public function view()
       {
         $tables = DB::select('SHOW TABLES');
         return view('view', [ 'tables' => $tables ]);
       }

        /**
        * @return \Illuminate\Support\Collection
        */
        public function export()
        {
          return Excel::download(new PeopleExport, 'people.xlsx');
         }

           /**
           * @return \Illuminate\Support\Collection
           */
           public function import()
           {
           $data = Excel::import(new PeopleImport,request()->file('file'));
           foreach($data->toArray() as $row) {
           $value[] = $row;
           foreach($row as $key => $val) {
                $fields[] = $key;
           }
           }
           $ff = array_unique($fields);

           return back()->withStatus(['تم الرفع']);
        }
   }

, кстати, я использую laravel 6 и maatwebsite 3.1

1 Ответ

0 голосов
/ 16 февраля 2020
return new People([
     if (!Schema::hasTable($table_name))

Вы не можете поместить if в массив return new People([if hasTable($table_name))...,

разместить его в другом месте ранее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...