DEV Community

Hamid Semix
Hamid Semix

Posted on


Getting started with yuga-framework part 2

In today's tutorial, I am going to take you through the steps required to build an actual blog application, I am sure you remember our first part was setting up an environmment.
Let's first list down what we need to do:

  • We need a database
  • we need a posts table
  • we need a comments table
  • we need a form for submitting a post (will be covered in part 3)
  • We need a page for displaying all posts (part 3)
  • We need a mechanism of comment on a post (part 3)
  • We need to save a post/comment (For now we will use a fairly raw approach)
  • We need to query the database for posts and their comments

Ok, let's get this over with, shall we?

First we need to make a connection to a database

Yuga comes with three database drivers integrated, the default one is mysql, to change the database to which our application connects, we will need to locate a file (environment/.env). This is the file that holds most of the framework's configurations. Let's name our database blog

Let's now create our tables in the database

To create the blog tables, we will use yuga's command for migrations which is php yuga migration:make.
We will have two tables for now which are posts and comments

Open the project in the terminal and type

php yuga migration:make posts

Do the same for comments, at this point, your database directory should like this
Migration Image

Let's run the command that creates these two tables

Before we create our tables in the database we earlier created, we need to figure out which fields need to go to each of the tables i.e. posts and comments

  • Posts table
    • id
    • creator_name
    • post_title
    • post_content
    • created_at
    • updated_at
  • Comments table
    • id
    • creator_name,
    • post_id
    • comment_content
    • created_at
    • updated_at

Let's put these fields with their appropriate data types in the migrations we just created

Now, In your terminal, enter this command

php yuga migration:up

This command will create the tables and now we should have the following
Migration done

Models for posts table and comments table

Now that we have our tables created, we need a way we can communicate with them in a flawless way, Luckly for us, yuga comes with an ORM for database manipulation, so let's create the Post model and the Comment model.
NB: Because our tables are posts and comments, this explains while our Models will be Post and Comment respectively as yuga converts the class name of the model and pluralizes and lowercases it, that's what it takes as the table (But customisable).

That's what we will use to save and retrieve data from the tables (models)

Let's use the yuga commands again for this task,

php yuga make:model Post

php yuga make:model Comment


Save data in our post table

To interact with our tables, we need a route which in the end calls a controller on our behalf. It's in the controller that we write our logic for saving our post to a database.
To create a controller we use php yuga make:controller BlogController command.

Let's open the created controller and create a method called createPost.

Now let's open routes/web.php file to create our route. This file has one route which is /, now let's add another route /post/create which we will use to create our blog post.

// routes/web.php

Route::all('/post/create', 'BlogController@createPost');

Controller Route

In the controller

// app/Controllers/BlogController.php

public function createPost(Post $post): string
        'creator_name' => 'Jone Doe',
        'post_title' => 'I believe every human has a finite number of heartbeats. I don\'t intend to waste any of mine.',
        'post_content' => 'Never in all their history have men been able truly to conceive of the world as one: a single sphere, a globe, having the qualities of a globe, a round earth in which all the directions eventually meet, in which there is no center because every point, or none, is center — an equal earth which all men occupy as equals. The airman\'s earth, if free men make it, will be truly round: a globe in practice, not in theory.',
    return 'Your Post has been saved';

You must have the following result when you vist http://localhost:8000/post/create


Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.