Courses

Vue.js 3 + Laravel 11 API + Vite: SPA CRUD

Category: API Resource with Relationship

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Setting up category model and relationships
- Creating category API resource
- Managing eager loading for categories
- Displaying related data in table view

In this lesson, let's show a column from the relationship in the posts table. We will add a category for every post.

finished category relation


First, we need to create a Category model and migration.

php artisan make:model Category -m

database/migrations/xxxx_create_categories_table.php:

public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

app/Models/Category.php:

class Category extends Model
{
protected $fillable = [
'name',
];
}

Next, we need to create a migration...

The full lesson is only for Premium Members.
Want to access all 27 lessons of this course? (115 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord