Overview
The Category model represents menu categories used to organize food items in the restaurant system. Categories provide a way to group similar dishes together for easier navigation and menu organization.
Database Table
Table Name: categories
Fillable Attributes
The category name (e.g., “Appetizers”, “Main Courses”, “Desserts”, “Beverages”)
Relationships
Has Many: Foods
A category can have multiple food items associated with it.
public function foods()
{
return $this->hasMany(Food::class);
}
Usage Example:
// Get all foods in a category
$category = Category::find(1);
$foods = $category->foods;
// Get category with food count
$categoriesWithCount = Category::withCount('foods')->get();
Model Definition
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $table = 'categories';
protected $fillable = ['name'];
// Relación: Una categoría tiene muchas comidas
public function foods()
{
return $this->hasMany(Food::class);
}
}
Database Schema
CREATE TABLE categories (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP NULL,
updated_at TIMESTAMP NULL
);
Usage Examples
Creating a Category
$category = Category::create([
'name' => 'Main Courses'
]);
Querying Categories
// Get all categories
$categories = Category::all();
// Get categories with food count
$categories = Category::withCount('foods')->get();
foreach ($categories as $category) {
echo "{$category->name}: {$category->foods_count} items";
}
// Get category with all its foods
$category = Category::with('foods')->find(1);
Updating a Category
$category = Category::find(1);
$category->update(['name' => 'Beverages & Drinks']);
Deleting a Category
$category = Category::find(1);
$category->delete();
Deleting a category may affect associated food items. Ensure you have proper foreign key constraints or cascading rules in place, or manually handle the relationship cleanup.
Common Queries
Get Popular Categories
// Categories with the most food items
$popularCategories = Category::withCount('foods')
->orderBy('foods_count', 'desc')
->take(5)
->get();
// Categories with at least one food item
$activeCategories = Category::has('foods')
->with(['foods' => function ($query) {
$query->latest()->take(10);
}])
->get();
Controller Usage
Categories are typically managed through the admin food management interface. See: