Writing a Blog in Laravel: View Layouts and Pagination

Naren Chitrakar
Sep 2, 2018 · 3 min read

Moving on. We are still using duplicate template, copy pasting the template and using them. We need to use and reuse a layout for the duplicate part of views.

To create a blog post, we are using the view resources/views/posts/create.blade.php. The content of which is

We have created the post but have not made a place to list those. Let’s create one.

We need to extract reusable part of the view into a base template, we can call them layouts. We can just replace the part that is replaced inside a section. The extracted layout section now looks like this. We create the file inside the views/layouts/ folder. The file is admin.blade.php

In this file we just extracted the reusable part of html and replaced the content that is not reusable with section named content, hence @section('content'). We need to use this layout in create.blade.php

Voila, we are using the laravel layout now.

Let’s list the posts now. To show the list , lets use the url posts. Let’s add the url in web.php

Route::get('posts','PostController@index')->middleware('auth');

Let’s create index method in PostController.

$request->users() fetches the currently logged in user and $users->posts() fetches the posts of that user. To use pagination, we can user paginate() method. The parameter passed is the posts per page. So, paginate(10) means 10 posts per page.

When we use paginate function, we can use the links method that generates the pagination links which defaults to bootsrap pagination links.

There are a lot of properties, you can see that neither exist in the table or we have not defined in the class. Laravel provides a neat way to add these properties to the model. For these to work, we need to have a method in the model in the format getAttributeAttribute So, for $post->url to work, we need to add a method getUrlAttribute in Post model.

public function getUrlAttribute()
{
return route('posts.single', $this->slug);
}

And for the route('posts.single', $this-slug) to work. We need to create a route named posts.single. These are called named routes. We can reference the route with a name and that makes it reusable and modifiable across the app. We add this route in web.php

Route::get('posts/{slug}','PostController@show')->name('posts.single');

After these changes, our web.php file looks like this

Let’s give name to all our urls and also we can group urls with same properties like prefix and middleware inside a group method of the Route class. After the changes the web.php looks like this

In our PostController let’s add the method show. Show method should fetch the post based on the slug and display to a view.

public function show(Post $post, $slug)
{
$post = $post->withSlug($slug)->first();

return view('posts.single', ['post' => $post]);
}

I have used a withSlug method to fetch the post with given slug. To make this work, we need to add a scope to our Post model.

public function scopeWithSlug($query, $slug)
{
return $query->whereSlug($slug);
}

Up until now, all our pages have title Create User because we use the same template for all our views. Let’s fix those. Let’s use a section named title for that. So in our layout file, we add a section title. Our layout file now look like this

and our create post view file looks like this

Now the @yield('title') is replaced with whatever we put inside @section('title') and @stop

I have added few other routes and views to the file to edit the posts. Those are pretty easy to understand. Let me know if you have any questions. These are the changes made during this post.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade