Models

Introduction

Model - This component is responsible for the data and also defines the structure of the application.

The model is the component responsible for working with application data. A model represents business logic, stores data, and makes it accessible from a controller or view.

In the context of web applications, the model can interact with the database, provide data validation, and execute and process business rules. The model may also include methods for reading, writing, updating, and deleting data from the database.

Using a model in MVC allows you to separate business logic from data display and user input. This makes the application easier to maintain and extend, reduces component coupling, and makes the code more readable and maintainable.

This framework implements an ORM system that allows you to easily access a table in a database. You can also use the query builder to create more complex database queries.

Creating Models

To generate the models you can use the nailer command:

php nailer make:model User

Using

In this example, we access the Users table to get all the records from it.

namespace App\Models;

use Src\Database\Model;

class Home extends Model
{
    public function __construct()
    {
        return $this->all();
    }
}

You can also initialize the class object in your controller.

namespace App\Controllers;

use App\Models\User;

class UserController extends Controller
{
    public function Index()
    {
        $User = new User;

        $Users = $User->all();

        return response($Users);
    }
}