Writing a Blog in Laravel: Create a Post — Part 1
We have created a work flow for our laravel application. For beginners it makes it easier to stick to the flow. We have built authentication based on the same steps. The steps to follow are:
- Decide on url of the application for the feature.
- Create the controller if it does not exist.
- Create the method in the controller if it does not exist
- Create the model, if it does not exist
- Call the model to fetch the data if needed.
- Create the view if it does not exist.
Lets follow this again to create a Post
The common practice to coming up with a url structures is to decide on a name and play around that with HTTP verbs. Since we are working with posts here. We can play with HTTP verbs with name posts. I like keeping it plural.
To create a post we need to work with two urls
- A url to display a form to create a post
- A url to post the form of the save to database
Displaying the form to create a post
- Decide on url of the application for the feature
Since we are displaying a form to create a post, lets use,posts/create
. Add this code toroutes.web.php
Route::get('posts/create', 'PostConroller@create');
This code says we are going to callPostController
andcreate
method in it. - Create the controller if it does not exist
We have decided to usePostController
andcreate
method in it. Since the controller does not exist, we need to create one.
$ php artisan make:controller PostController
This will create a file app/Controllers/PostController.php
3. Create the method in the controller if it does not exist
Add the code
public function create(){}
to the PostController
4. Create the model, if it does not exist
We are creating posts. We need a table to store posts.
To create a model, we need to run the command
$ php artisan make:model Post -m
The -m
flag also create a migration files for use
This created a file
database/migrations/2018_08_15_171530_create_posts_table.php with content
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
We would need to add a few fields to make this to work. A post would at least have a title, content, slug ( a unique string identifier for a post ) and an author. Let’s add those.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('content');
$table->string('slug')->unique();
$table->integer('user_id')->unsigned()->index()->comment('author of the post');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
After editing the migration just run the command
$ php artisan migrate
This command will create a posts
table with the mentioned columns in the table.
5. Call the model to fetch the data if needed.
Since we are just displaying a form to create a user. We do not need to get data from posts
table. So we can skip this
6. Create the view if it does not exist.
We need a view to display the form. We need to insert title and content. Slug should be created automatically based on title.
Create a file resources/views/posts/create.blade.php
. Create a from with these fields.
To call this view we need to add the code PostController
.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function create()
{
return view('users.create');
}
}
The end result of the code should look like.
The changes made in this code is here.