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) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->text('template');
            $table->timestamps();
            $table->timestamp('published_at');
        });
    }

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 = [
        'title',
        'body',
        'published_at',
        'template'
    ];

    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

 'required|min:3',
            '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 authorize to true. The rules section is telling Laravel what is required from filling in the form and any min or mac chars needed etc etc.

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

php artisan make:controller ArticlesController

published()->get();
        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) 
    {
    	articles::create($request->all());
    	return redirect('/articles');
    }
    //Show full article
    public function show($id)
    {
        $articles = articles::findOrFail($id);
        return view('Articles.show' , 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 and 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 your 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']) !!}
Template 
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 a 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. The Part 3 we will add images.


Created: 2016-08-29.


Recent posts


Basic Laravel install

So you want to set up a new Laravel Install? Correct? Right ok so lets begin..... First...

2017-06-15

Read More

Back to blogging and a headache

It's a hard thing this day in age to decide what is best to do in terms of technology and how...

2017-05-23

Read More

Twitter