Overview
The Page model represents custom static pages in LaraCMS. It provides a simple way to create and manage standalone content pages like “About Us”, “Contact”, “Privacy Policy”, etc.
Namespace: App\Models\Page
Extends: Illuminate\Database\Eloquent\Model
Properties
Fillable Attributes
URL-friendly identifier for the page (used in routes)
The main content/body of the page (supports HTML)
Publication status - determines if the page is visible to the public
Usage Examples
Creating a Page
use App\Models\Page;
$page = Page::create([
'title' => 'About Us',
'slug' => 'about-us',
'content' => '<h1>About Our Company</h1><p>We are a leading...</p>',
'published' => true,
]);
Finding Pages
// Find by ID
$page = Page::find(1);
// Find by slug
$page = Page::where('slug', 'about-us')->first();
// Get all published pages
$publishedPages = Page::where('published', true)->get();
// Get all unpublished pages
$drafts = Page::where('published', false)->get();
Using Route Model Binding
// In routes/web.php
Route::get('/pages/{page:slug}', [PageController::class, 'show']);
// In controller
public function show(Page $page)
{
// $page is automatically loaded by slug
return view('pages.show', compact('page'));
}
// URL: /pages/about-us
Updating Pages
$page = Page::find(1);
$page->update([
'title' => 'Updated About Us',
'content' => '<h1>Updated Content</h1>',
]);
// Publish a draft
$page->update(['published' => true]);
// Unpublish a page
$page->update(['published' => false]);
Creating Draft Pages
// Create as draft (unpublished)
$draft = Page::create([
'title' => 'New Feature Announcement',
'slug' => 'new-feature',
'content' => '<p>Coming soon...</p>',
'published' => false,
]);
// Publish later
$draft->update(['published' => true]);
Searching Pages
// Search by title
$pages = Page::where('title', 'like', '%About%')->get();
// Search by content
$pages = Page::where('content', 'like', '%company%')->get();
// Search in both title and content
$searchTerm = 'privacy';
$pages = Page::where('title', 'like', "%{$searchTerm}%")
->orWhere('content', 'like', "%{$searchTerm}%")
->get();
Ordering Pages
// Get newest pages first
$pages = Page::orderBy('created_at', 'desc')->get();
// Get alphabetically by title
$pages = Page::orderBy('title', 'asc')->get();
// Get recently updated
$pages = Page::orderBy('updated_at', 'desc')->get();
// Paginate all published pages
$pages = Page::where('published', true)
->orderBy('title')
->paginate(10);
// In view
@foreach($pages as $page)
<div>
<h2>{{ $page->title }}</h2>
<div>{!! $page->content !!}</div>
</div>
@endforeach
{{ $pages->links() }}
Bulk Operations
// Publish multiple pages
Page::whereIn('id', [1, 2, 3])->update(['published' => true]);
// Unpublish all pages
Page::query()->update(['published' => false]);
// Delete unpublished pages older than 30 days
Page::where('published', false)
->where('created_at', '<', now()->subDays(30))
->delete();
Checking Publication Status
$page = Page::find(1);
if ($page->published) {
// Page is published
echo "This page is live";
} else {
// Page is a draft
echo "This page is not published yet";
}
Deleting Pages
$page = Page::find(1);
$page->delete();
// Or delete by slug
Page::where('slug', 'about-us')->delete();
// Soft delete (if using soft deletes trait)
// Would require adding SoftDeletes trait to the model
// Get all published pages for navigation
$menuPages = Page::where('published', true)
->orderBy('title')
->get(['id', 'title', 'slug']);
// In blade template
<nav>
@foreach($menuPages as $page)
<a href="{{ route('pages.show', $page->slug) }}">
{{ $page->title }}
</a>
@endforeach
</nav>
// In PageController
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|max:255',
'slug' => 'required|unique:pages|max:255|alpha_dash',
'content' => 'required',
'published' => 'boolean',
]);
$page = Page::create($validated);
return redirect()->route('pages.show', $page->slug);
}
public function update(Request $request, Page $page)
{
$validated = $request->validate([
'title' => 'required|max:255',
'slug' => 'required|max:255|alpha_dash|unique:pages,slug,' . $page->id,
'content' => 'required',
'published' => 'boolean',
]);
$page->update($validated);
return redirect()->route('pages.show', $page->slug);
}
Automatic Slug Generation
use Illuminate\Support\Str;
// In controller or model observer
$page = new Page();
$page->title = 'About Our Company';
$page->slug = Str::slug($page->title); // 'about-our-company'
$page->content = $request->content;
$page->published = true;
$page->save();
Database Schema
The pages table includes:
id - Primary key
title - Page title
slug - URL-friendly slug (unique)
content - Page content (text/HTML)
published - Boolean for publication status
created_at - Timestamp
updated_at - Timestamp
Best Practices
- Unique Slugs: Always ensure slugs are unique across all pages
- Slug Format: Use lowercase letters, numbers, and hyphens only
- Content Sanitization: Sanitize HTML content to prevent XSS attacks
- Published Flag: Use the published field to control visibility instead of deleting drafts
- SEO: Consider adding meta description and keywords fields for better SEO
Extending the Model
Consider adding these features:
// Add to model for automatic slug generation
protected static function boot()
{
parent::boot();
static::creating(function ($page) {
if (empty($page->slug) && !empty($page->title)) {
$page->slug = Str::slug($page->title);
}
});
}
// Add scope for published pages
public function scopePublished($query)
{
return $query->where('published', true);
}
// Usage:
$pages = Page::published()->get();