Graham Morby-Raybould

Designer. Developer. Freelancer.

Laravel 5.2 Blog with multiple templates Part One

Laravel 5.2 Blog with multiple templates Part One

So after some deep thought very early one morning I was hoping to be able to load not only this blog but others with multiple templates to display different types of posts. So I have the choice of a Full Width View and a Sidebar View when creating posts, so let's get started.

First thing we need to do is create our migration:

php artisan make:migration create_articles_table --create=articles

This will create a new migration file, this file can be found in Databases->Migrations->create_articles_table

public function up()
        Schema::create('articles', function (Blueprint $table) {

So as you can see from the above code we have a Title, Body for our content, a published date and the template column. If we now go ahead and run 

php artisan migrate

This will push up our new table to our database. Now we need to create our model for the articles table. So again off we pop to artisan

php artisan make:model articles

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class articles extends Model
    protected $fillable = [

    protected $dates = ['published_at'];
	//Get all published articles by date
    public function scopePublished($query)
		$query->where('published_at' , '<=' , Carbon::now());
	//Get all unpublished or future articles
    public function scopeUnpublished($query)
		$query->where('published_at' , '>=' , Carbon::now());
	// Set form to publish articles with a time and date in the Published_at field
    public function setPublichedAtAttribute($date)
		$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d' , $date);

So the model is making sure we have fillable fields and setting the correct time and date for our published at field. We also have our database queries for getting the lastest posts and the magic one of setting future posts aswell. 

The last thing for the back end part will be the request so again hop back into terminal or cmd and go for 

php artisan make:request ArticlesRequest

            'body' => 'required',
            'published_at' => 'required|date',
            'template' => 'required'

Ok easy one here, I want to make sure all users and such can connect so I set the authorise to true. The rules section is telling Laravel what is required from filling in the form and any min or mac chars needed etc .

Ok so lets set the controllers, back into bash or cmd

php artisan make:controller ArticlesController

        return view('Articles.index' , compact('articles'));
    // Create an article view
    public function create()
    	return view('Articles.create');
    //Store an article
    public function store(ArticleRequest $request) 
    	return redirect('/articles');
    //Show full article
    public function show($id)
        $articles = articles::findOrFail($id);
        return view('' , compact('articles'));

So as you can see we have a create function which returns us our form view, a index page that fetches all the articles and displays them to the screen. The store funtion is the restful part and holds the form and sends it to our database and last but not least our show function which allows us to view a single article.

So if we create our create view within views->articles->create.blade.php, extend whatever Blade templates you're using and stylesheet, we can create the form. I'm using bootstrap for this example.

{!! Form::label('title' , 'Title:') !!} {!! Form::text('title', null , ['class' => 'form-control']) !!}

{!! Form::label('body' , 'Content:') !!} {!! Form::textarea('body', null , ['class' => 'form-control']) !!}

{!! Form::label('published_at' , 'Publish On:') !!} {!! Form::input('date' , 'published_at', date('Y-m-d') , ['class' => 'form-control']) !!}
If you are using a full width template, the recommended image size is 1170px x 450px

{!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}
{{ Form::submit('Delete', ['class' => 'btn btn-danger form-control']) }}

And there is our form, we have an input for each type and also a select box bor our templates. So that should get you to a point where you can upload a record to the database and store all the information for a blog post. In part 2 we will look to add our elements to the layout and edit, update and delete blog posts. Then Part 3 we will add images.

Created: 2017-11-14.

Recent posts

OFF TOPIC - The last Cassini image is something so beautiful

We live in a world where technology rules, a world where we cant seem to cross the road with out...


Read More

Have you tried turning it off and on again

It would seem its a common thing this day in age. As a developer I have a certain amount of compu...


Read More