View Layouts and Pagination in Laravel

1. Create a Layout:

Create a layout file named admin.blade.php in the resources/views/layouts/ directory. This layout will contain the common structure of your pages.

<!-- resources/views/layouts/admin.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>@yield('title')</title>
    <!-- Add your CSS and JavaScript links here -->
</head>
<body>
    <header>
        <!-- Your header content goes here -->
    </header>
    <nav>
        <!-- Your navigation menu goes here -->
    </nav>
    <div class="content">
        @yield('content')
    </div>
    <footer>
        <!-- Your footer content goes here -->
    </footer>
</body>
</html>

2. Modify Your Create View:

Modify your create.blade.php view file to extend the admin layout and provide a title section.

<!-- resources/views/posts/create.blade.php -->
@extends('layouts.admin')

@section('title', 'Create a Blog Post')

@section('content')
    <!-- Your create post content goes here -->
    <form>
        <!-- Your form fields go here -->
    </form>
@endsection

3. List Posts (Index Page):

Create a new view file for listing posts, e.g., index.blade.php. Extend the admin layout, set the title, and define the content section.

<!-- resources/views/posts/index.blade.php -->
@extends('layouts.admin')

@section('title', 'Blog Posts')

@section('content')
    <!-- Your list of blog posts goes here -->
    @foreach($posts as $post)
        <!-- Display individual posts here -->
    @endforeach

    <!-- Pagination links go here -->
    {{ $posts->links() }}
@endsection

4. Define Routes:

Define routes for creating and listing blog posts in your web.php file.

// Define the route for creating a blog post
Route::get('posts/create', 'PostController@create')->middleware('auth');

// Define the route for listing blog posts
Route::get('posts', 'PostController@index')->middleware('auth');

5. Controller Methods:

In your PostController, add methods for creating and listing blog posts.

public function create()
{
    return view('posts.create');
}

public function index()
{
    // Fetch paginated blog posts (e.g., 10 posts per page)
    $posts = Post::paginate(10);

    return view('posts.index', compact('posts'));
}

6. Title Attribute for Posts:

In your Post model, define an accessor to generate the title for each post and a named route for viewing a single post.

// Post.php
public function getTitleAttribute()
{
    // Customize how you generate the title for each post
    return 'Blog Post: ' . $this->title;
}

// Define a named route for viewing a single post
Route::get('posts/{slug}', 'PostController@show')->name('posts.show');

7. View Single Post:

Create a view file named single.blade.php for viewing a single post.

<!-- resources/views/posts/single.blade.php -->
@extends('layouts.admin')

@section('title', $post->title)

@section('content')
    <!-- Display the single post content here -->
    <h1>{{ $post->title }}</h1>
    <p>{{ $post->content }}</p>
@endsection

In your PostController, add a method to display a single post.

public function show($slug)
{
    $post = Post::withSlug($slug)->firstOrFail();

    return view('posts.single', compact('post'));
}

8. Pagination Links:

Laravel’s pagination links are generated automatically when you use paginate(). Include {{ $posts->links() }} in your index.blade.php to display the pagination links.

Related Posts

Evolution of Platform Engineering and Data-Driven Software Delivery Practices

Introduction In the modern technology ecosystem, the capability to deliver software rapidly, reliably, and securely is a definitive competitive advantage. Finding and implementing the Best DevOps Tools…

Read More

Adventure Activities in India: Top Places to Explore

Introduction India is less of a single country and more of a vibrant, sensory-rich continent bound together by shared history and deep-rooted traditions. For any global traveler,…

Read More

Streamlining Automated Data Pipelines Using Enterprise DataOps Best Practices

Introduction In modern cloud environments, businesses generate massive amounts of information every single second. Managing this information manually creates massive operational bottlenecks, delays business intelligence insights, and…

Read More

Modern DataOps Infrastructure: Unlocking the Power of Observability Platforms

Introduction Modern enterprise data architectures are growing increasingly complex. Today, an ordinary business analytics pipeline might ingest streaming IoT logs, batch-load transactional customer databases, transform those layers…

Read More

Elevating DevSecOps and SRE Efficiency with a Software Delivery Governance Platform

Introduction Enterprise software engineering has reached a tipping point where systemic complexity threatens structural delivery stability. Modern engineering organizations routinely support highly fragmented ecosystems populated by hundreds…

Read More

Best Hospitals in India for International Patients and Affordable Surgery Costs

Introduction Global healthcare costs are rising rapidly, forcing many families to look for alternative solutions when facing serious medical diagnoses. In countries like the United States, the…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x