You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use code below for import all models at the same time from different excel files.
It works well on Laravel 7.
But when I try to move it to Laravel 8, I always get error PDOException There is no active transaction
I see in debugbar that first import of Feedback model is done and after that error is fired.
I cannot understand the reason of this problem. Documentation seems the same.
<?php
namespace App\Admin\Controllers;
use Illuminate\Support\Facades\DB;
use App\Imports\Feedback\FeedbackImport;
use App\Imports\News\NewsImport;
use App\Imports\Menu\MenuImport;
use Maatwebsite\Excel\Facades\Excel;
class SeedController extends BaseAdminController
{
public $feedback;
public $news;
public $menu;
public function __construct()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
$this->feedback = new FeedbackImport();
$this->news = new NewsImport();
$this->menu = new MenuImport();
}
public function index(Content $content)
{
$this->import();
$feedback = $this->feedback;
$news = $this->news;
$menu = $this->menu;
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
return $content
->title('Import & Export')
->description('Laravel excel')
->view('admin.seed', compact('menu', 'news', 'feedback'))
->withSuccess('Import succesfully finished');
}
public function import()
{
Excel::import($this->feedback, 'import/feedback.xlsm');
Excel::import($this->news, 'import/news.xlsm');
Excel::import($this->menu, 'import/menu.xlsm');
}
}
App\Imports\Feedback\FeedbackImport.php
<?php
namespace App\Imports\Feedback;
use App\Models\Feedback;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\AfterSheet;
class FeedbackImport implements ToModel, WithHeadingRow, WithEvents
{
use RegistersEventListeners;
public $rows = 0;
public function model(array $row)
{
++$this->rows;
return new Feedback([
'name' => $row['name'],
'rate' => $row['rate'],
'text' => $row['text'],
]);
}
public function getRowCount(): int
{
return $this->rows;
}
public static function beforeSheet(BeforeSheet $event)
{
app('log')->info('Feedback import started');
Feedback::truncate();
}
public static function afterSheet(AfterSheet $event)
{
app('log')->info('Feedback import finished');
}
}
App\Imports\News\NewsImport.php
<?php
namespace App\Imports\News;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Events\BeforeImport;
use Maatwebsite\Excel\Events\AfterImport;
class NewsImport implements WithMultipleSheets, WithEvents
{
use RegistersEventListeners;
public $category;
public $post;
public function __construct()
{
$this->category = new NewsImportCategory();
$this->post = new NewsImportPost();
}
public function sheets(): array
{
return [
'news_categories' => $this->category,
'news_posts' => $this->post,
];
}
public static function beforeImport(BeforeImport $event)
{
app('log')->info('News import started');
}
public static function afterImport(AfterImport $event)
{
app('log')->info('News import finished');
}
}
App\Imports\News\NewsImportCategory.php
<?php
namespace App\Imports\News;
use App\Models\NewsCategory;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\AfterSheet;
class NewsImportCategory implements ToModel, WithHeadingRow, WithEvents
{
use RegistersEventListeners;
public $rows = 0;
public function model(array $row)
{
if (empty($row['id'])) { return null; }
++$this->rows;
return new NewsCategory([
'id' => $row['id'],
'parent_id' => $row['parent_id'],
'title' => $row['title'],
'description' => $row['description'],
]);
}
public function getRowCount(): int
{
return $this->rows;
}
public static function beforeSheet(BeforeSheet $event)
{
app('log')->info('NewsCategory import started');
NewsCategory::truncate();
}
public static function afterSheet(AfterSheet $event)
{
app('log')->info('NewsCategory import finished');
}
}
App\Imports\News\NewsImportPost.php
<?php
namespace App\Imports\News;
use App\Models\NewsPost;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\AfterSheet;
class NewsImportPost implements ToModel, WithHeadingRow, WithEvents
{
use RegistersEventListeners;
public $rows = 0;
public function model(array $row)
{
if (empty($row['category_id'])) { return null; }
++$this->rows;
return new NewsPost([
'category_id' => $row['category_id'],
'title' => $row['title'],
'fulltext' => $row['fulltext'],
'image' => $row['image'],
]);
}
public function getRowCount(): int
{
return $this->rows;
}
public static function beforeSheet(BeforeSheet $event)
{
app('log')->info('NewsPost import started');
NewsPost::truncate();
}
public static function afterSheet(AfterSheet $event)
{
app('log')->info('NewsPost import finished');
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I use code below for import all models at the same time from different excel files.
It works well on Laravel 7.
But when I try to move it to Laravel 8, I always get error
PDOException There is no active transaction
I see in debugbar that first import of Feedback model is done and after that error is fired.
I cannot understand the reason of this problem. Documentation seems the same.
App\Admin\routes.php
App\Admin\Controllers\SeedController.php
App\Imports\Feedback\FeedbackImport.php
App\Imports\News\NewsImport.php
App\Imports\News\NewsImportCategory.php
App\Imports\News\NewsImportPost.php
Menu is almost same as News import.
Beta Was this translation helpful? Give feedback.
All reactions