added all_news_view

This commit is contained in:
amikhaylov
2026-06-13 22:33:41 +03:00
parent d5f947d59e
commit 71c34cee77
4 changed files with 43 additions and 2 deletions
+1 -1
View File
@@ -6,6 +6,6 @@ use Illuminate\Database\Eloquent\Model;
class News extends Model class News extends Model
{ {
protected $table = 'allNewsView'; protected $table = 'news';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
} }
@@ -31,7 +31,7 @@ return new class extends Migration
// Настройка движка и кодировки (опционально, если нужно строго как в SQL) // Настройка движка и кодировки (опционально, если нужно строго как в SQL)
$table->engine = 'InnoDB'; $table->engine = 'InnoDB';
$table->charset = 'utf8mb3'; // $table->charset = 'utf8mb3';
}); });
} }
@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
// Сначала удаляем, если уже существует
DB::statement("DROP VIEW IF EXISTS `all_news_view` ");
// Создаем вьюху
DB::statement("
CREATE VIEW `all_news_view` AS
SELECT
`id`,
DATE_FORMAT(`date`, '%m') AS `month`,
DATE_FORMAT(`date`, '%d') AS `day`,
DATE_FORMAT(`date`, '%H:%i') AS `time`,
`header`,
`long_text` AS `article`,
`from_id` AS `author`,
`img_src`,
`author_type`,
`copy_post_id`,
`post_type`
FROM
`news`
WHERE
`long_text` <> ''
ORDER BY `date` DESC
");
}
public function down(): void
{
DB::statement("DROP VIEW IF EXISTS `all_news_view` ");
}
};
+1
View File
@@ -24,6 +24,7 @@ class DatabaseSeeder extends Seeder
$this->call([ $this->call([
UserSeeder::class, UserSeeder::class,
EventSeeder::class,
]); ]);
} }
} }