Laravel 5.1 Beauty - Posts Administration

Finishing out the posts administration area

In this chapter we’ll finish the post functionality in the blog’s administration area. This includes modifying the structure of the posts table with a new migration, pulling in some additional assets, and adding the basic Create, Update, and Delete methods.

Modifying the Posts table

The nice thing about database migrations in Laravel 5.1 is that they’re like version control for your database. Right now the version of the posts table still exists from when we created the 10 Minute Blog a few chapters back.

Let’s make some changes to this table.

Requiring Doctrine

In Laravel 5.1 whenever columns in a database need to be modified, the Doctrine package is required. Use composer to install the doctrine/dbal package as instructed below.

Here we set the name of the columns that can be filled with an array. The addNeededTags() method will use this.

posts()

The many-to-many relationship between posts and tags.

addNeededTags()

A static function to add tags that aren’t in the database already.

In the posts() method we’re only passing two arguments to
belongsToMany(). The first argument is the name of the model class. The
second argument is the name of the table to use. The next two arguments are
the foreignKey and the otherKey, but since we’re using post_id and
tag_id, the additional arguments to belongsToMany() can be omitted.
(Laravel is smart enough to figure them out.)

Mass Assignment Protection

Laravel 5.1’s models are built with Mass Assignment Protection. This
means the model’s create() method takes an array of column names and
values, but only allows assignment of those columns that are in a white
list. (The white list is the $fillable attribute).

Similar to the posts() method in the Tag model, but here we’re going the other way.

setTitleAttribute()

We changed this from the previous version to call the setUniqueSlug() method.

setUniqueSlug()

A function that recurses to set the unique slug when needed.

setContentRawAttribute()

Now when the content_raw value is set in the model, it will be automatically be converted to HTML and assigned to the content_html attribute.

syncTags()

Synchronizes the tags with the post.

Adding Selectize.js and Pickadate.js

Let’s add two additional assets to our system. We’ll use bower to pull the resources in and gulp to put them where we want them.

Pulling them in with Bower

The first is Selectize.js. This library is handy for setting up tagging and we’ll use to to assign zero or more Tags to each Post. Follow the instructions below to pull it in with bower. (I’m doing this from the Host OS, but you should also be able to do this within Homestead VM.)

Then let’s pull in Pickadate.js. There’s quite a few libraries for picking dates and times available, but I wanted to use this one because it works slick on small devices. Follow the instructions below to pull in Pickadate.js.

Content of PostUpdateRequest.php

NOTE: We’re just inherting the authorize() and rules() methods from the PostCreateRequest class. Yes, we could get by with a single class to handle both but I don’t know how things may change in the future and like the idea of separate classes.

Creating the PostFormFields Job

Let’s create a utility job we can call from the PostController. It’ll be called the PostFormFields job. This job will get executed when we want to get a list of all the fields to populate post form.

Laravel Job Classes are Useful

Whenever you want to encapsulate a bit of action into its own class, a
job class is one way to go. You dispatch to the job and don’t have
to worry about the details. You can even queue jobs to occur later.

Just like helpers.php contain one-off functions, I like to think of
job classes as one-off action classes.

This helper function will be used in views to output the checked attribute in check boxes and radio buttons.

page_image()

This function returns the full path to an image in the uploaded area using the value from the configuration. If a value isn’t specified then it pulls a default image from the blog config (which you’ll need to set up yourself in as ‘page_image’ if you wish to use.)

Updating the Post Model

You may have noticed how we’re breaking apart the published_at into publish_date and publish_time. Let’s add a couple fields to the Post model to make this easy. Update app/Post.php as specified below.

Removing the show route

The last thing to do before the administration area is complete is to remove the post show route.

Edit app/Http/routes.php, editing it as instructed below.

Removing the admin.post.show route

// Find the following line
resource('admin/post', 'PostController');
// Change it to this
resource('admin/post', 'PostController', ['except' => 'show']);

Congratulations!

The administration area of your blog is complete. Try it out. Add a few
posts, edit them. Try deleting them.

The display of the blog pages doesn’t look beautiful yet, we’ll get to
that next.

Recap

This was a fairly long chapter but a huge amount was accomplished. We created a migration to modify the posts table and then updated the Tag and Post models. Then bower was used to pull in the Selectize.js and Pickadate.js libraries which, of course, we managed using gulp.

Request classes (to handle form input) were created. As was a Laravel Job class to return the post form data. Finally, the controller and views were wrapped up.