{
  "slug": "consume-laravel-da",
  "title": "Forbrug fra Laravel (PHP)",
  "description": "Læs @webhouse/cms-indhold fra en Laravel-applikation. Hjælpeklasse, routes, Blade-skabeloner.",
  "category": "consumers",
  "order": 10,
  "locale": "da",
  "translationGroup": "a77b58cf-ebf1-4e7d-a9c3-67c1f5639f37",
  "helpCardId": null,
  "content": "## Opsætning\n\nPlacér dit Laravel-projekt og @webhouse/cms-indhold side om side:\n\n```\nmit-projekt/\n  cms.config.ts      # Indholdsmodel\n  content/           # JSON-dokumenter (læses af Laravel)\n  public/uploads/    # Mediefiler (serveres af Laravel)\n  app/               # Din Laravel-app\n  resources/views/   # Blade-skabeloner\n```\n\n## Hjælpeklasse\n\nOpret `app/Services/Webhouse.php`:\n\n```php\n<?php\n\nnamespace App\\Services;\n\nuse Illuminate\\Support\\Facades\\File;\nuse Illuminate\\Support\\Collection;\n\nclass Webhouse\n{\n    public static function contentDir(): string\n    {\n        return base_path('content');\n    }\n\n    /**\n     * List alle publicerede dokumenter i en collection.\n     */\n    public static function collection(string $collection, ?string $locale = null): Collection\n    {\n        $dir = self::contentDir() . '/' . $collection;\n        if (!File::isDirectory($dir)) {\n            return collect();\n        }\n\n        return collect(File::files($dir))\n            ->filter(fn($f) => $f->getExtension() === 'json')\n            ->map(fn($f) => json_decode(File::get($f->getPathname()), true))\n            ->filter(fn($d) => ($d['status'] ?? null) === 'published')\n            ->when($locale, fn($c) => $c->filter(fn($d) => ($d['locale'] ?? 'en') === $locale))\n            ->sortByDesc(fn($d) => $d['data']['date'] ?? '')\n            ->values();\n    }\n\n    /**\n     * Hent et enkelt dokument via slug.\n     */\n    public static function document(string $collection, string $slug): ?array\n    {\n        $path = self::contentDir() . \"/{$collection}/{$slug}.json\";\n        if (!File::exists($path)) return null;\n        $doc = json_decode(File::get($path), true);\n        return ($doc['status'] ?? null) === 'published' ? $doc : null;\n    }\n}\n```\n\n## Routes\n\n```php\n// routes/web.php\nuse App\\Services\\Webhouse;\nuse Illuminate\\Support\\Facades\\Route;\n\nRoute::get('/', function () {\n    $posts = Webhouse::collection('posts', 'da');\n    return view('home', ['posts' => $posts]);\n});\n\nRoute::get('/blog/{slug}', function (string $slug) {\n    $post = Webhouse::document('posts', $slug);\n    abort_unless($post, 404);\n    return view('post', ['post' => $post]);\n});\n```\n\n## Blade-skabelon\n\n```blade\n{{-- resources/views/post.blade.php --}}\n@extends('layouts.app')\n\n@section('content')\n  <article>\n    <h1>{{ $post['data']['title'] }}</h1>\n    <time>{{ $post['data']['date'] ?? '' }}</time>\n    <div class=\"prose\">\n      {!! \\Illuminate\\Support\\Str::markdown($post['data']['content'] ?? '') !!}\n    </div>\n    @foreach (($post['data']['tags'] ?? []) as $tag)\n      <a href=\"/tags/{{ $tag }}\" class=\"tag\">#{{ $tag }}</a>\n    @endforeach\n  </article>\n@endsection\n```\n\n## Servering af uploadede medier\n\n`@webhouse/cms` gemmer uploadede medier i `public/uploads/`. Laravel serverer allerede `public/`-mappen, så `/uploads/mit-billede.jpg` virker bare.\n\n## i18n: læs begge sprog\n\n```php\n// Hent alle danske indlæg\n$daPosts = Webhouse::collection('posts', 'da');\n\n// Hent alle engelske indlæg\n$enPosts = Webhouse::collection('posts', 'en');\n\n// Find oversættelsen af et specifikt indlæg\n$post = Webhouse::document('posts', 'hello-world');\n$translationGroup = $post['translationGroup'] ?? null;\n$translation = Webhouse::collection('posts')\n    ->firstWhere(fn($d) => ($d['translationGroup'] ?? null) === $translationGroup && ($d['locale'] ?? null) !== $post['locale']);\n```\n\n## Caching\n\nI produktion, cache parsed JSON:\n\n```php\nuse Illuminate\\Support\\Facades\\Cache;\n\npublic static function collection(string $collection, ?string $locale = null): Collection\n{\n    return Cache::remember(\"webhouse:{$collection}:{$locale}\", 60, function () use ($collection, $locale) {\n        // ... samme logik som før\n    });\n}\n```\n\nRyd cachen når indhold ændres — enten via en fil-watcher eller et CMS admin webhook.\n\n## Næste skridt\n\n- Se [Laravel-eksemplet](https://github.com/webhousecode/cms/tree/main/examples/consumers/laravel-blog)\n- Læs om [i18n](/docs/i18n) for flersprogsmønstre\n- Læs om [Framework-agnostisk arkitektur](/docs/framework-agnostic)",
  "excerpt": "Opsætning\n\nPlacér dit Laravel-projekt og @webhouse/cms-indhold side om side:\n\n\nmit-projekt/\n  cms.config.ts       Indholdsmodel\n  content/            JSON-dokumenter (læses af Laravel)\n  public/uploads/     Mediefiler (serveres af Laravel)\n  app/                Din Laravel-app\n  resources/views/    ",
  "seo": {
    "metaTitle": "Forbrug fra Laravel (PHP) — webhouse.app Docs",
    "metaDescription": "Læs @webhouse/cms-indhold fra en Laravel-applikation. Hjælpeklasse, routes, Blade-skabeloner.",
    "keywords": [
      "webhouse",
      "cms",
      "laravel",
      "php",
      "forbruger"
    ]
  },
  "createdAt": "2026-04-08T12:00:00.000Z",
  "updatedAt": "2026-04-08T12:00:00.000Z"
}