<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('pages', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug')->unique();
$table->json('fields')->nullable(); // chứa các trường tùy biến
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('pages');
}
};
public function store(Request $request)
{
$raw = json_decode($request->input('fields_json'), true);
Page::create([
'title' => $request->input('page_title'),
'slug' => Str::slug($request->input('page_title')),
'fields' => $raw, // ✅ Đây là format bạn muốn
]);
return redirect()->route('pages.index')->with('success', 'Tạo trang thành công!');
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Page extends Model
{
protected $casts = [
'fields' => 'array',
];
protected $fillable = [
'title',
'slug',
'fields'
];
}