Creating Entity.php

This PHP code represents a Laravel model named Invoice within the Modules\Account\Entities namespace

<?php

namespace Modules\Account\Entities;

use Illuminate\Database\Schema\Blueprint;
use Modules\Base\Classes\Migration;
use Modules\Base\Entities\BaseModel;

class Invoice extends BaseModel
{
    /**
     * The fields that can be filled
     *
     * @var array<string>
     */
    protected $fillable = [
        'title', 'invoice_no', 'partner_id', 'due_date', 'module', 'model', 'status',
        'description', 'is_posted', 'total',
    ];

    /**
     * The fields that are to be render when performing relationship queries.
     *
     * @var array<string>
     */
    public $rec_names = ['title'];

    /**
     * List of tables names that are need in this model during migration.
     *
     * @var array<string>
     */
    public array $migrationDependancy = ['partner'];

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = "account_invoice";

    /**
     * Set if model is visible from frontend.
     *
     * @var bool
     */
    public bool $show_frontend = true;

    /**
     * List of fields to be migrated to the datebase when creating or updating model during migration.
     *
     * @param Blueprint $table
     * @return void
     */
    public function fields(Blueprint $table = null): void
    {
        $this->fields = $table ?? new Blueprint($this->table);

        $statuses = ['draft' => 'Draft', 'pending' => 'Pending', 'partial' => 'Partial', 'paid' => 'Paid', 'closed' => 'Closed', 'void' => 'Void'];
        $statuses_color = ['draft' => 'gray', 'pending' => 'sky', 'partial' => 'indigo', 'paid' => 'green', 'closed' => 'orange', 'void' => 'red'];

        $this->fields->increments('id')->html('hidden');
        $this->fields->string('title')->html('text');
        $this->fields->char('invoice_no', 100)->html('text');
        $this->fields->foreignId('partner_id')->html('recordpicker')->relation(['partner']);
        $this->fields->date('due_date')->useCurrent()->html('datetime');
        $this->fields->string('module')->default('Account')->html('text');
        $this->fields->string('model')->default('Invoice')->html('text');
        $this->fields->enum('status', array_keys($statuses))->html('switch')->default('draft')->options($statuses)->color($statuses_color)->nullable();
        $this->fields->string('description')->nullable()->html('textarea');
        $this->fields->tinyInteger('is_posted')->default(0)->html('switch');
        $this->fields->decimal('total', 20, 2)->nullable()->html('amount');
    }

    /**
     * List of structure for this model.
     */
    public function structure($structure): array
    {
        $structure['table'] = ['title', 'invoice_no', 'partner_id', 'due_date', 'status', 'is_posted', 'total'];
        $structure['form'] = [
            ['label' => 'Invoice Title', 'class' => 'col-span-full', 'fields' => ['title']],
            ['label' => 'Invoice Details', 'class' => 'col-span-full md:col-span-6 md:pr-2', 'fields' => ['invoice_no', 'partner_id', 'due_date']],
            ['label' => 'Invoice Other Setting ', 'class' => 'col-span-full md:col-span-6 md:pr-2', 'fields' => ['status', 'is_posted', 'total']],
        ];
        $structure['filter'] = ['title', 'invoice_no', 'partner_id', 'due_date', 'status'];

        return $structure;
    }

}

. Let's break down the key components and functionalities of this model:

  1. Namespace: The Invoice class is defined within the Modules\Account\Entities namespace, which indicates that it belongs to a module named "Account" under the "Entities" directory.

  2. Extends BaseModel: The Invoice class extends BaseModel, suggesting that it inherits properties and methods from the BaseModel class. This is a common practice in Laravel for creating custom base models with shared functionalities.

  3. $fillable Property: This property defines an array of fields that are mass assignable. Mass assignment allows you to assign multiple attributes to a model using an array, typically when creating or updating records in the database.

  4. $rec_names Property: This property specifies an array of field names that are to be rendered when performing relationship queries. It might be used for displaying concise representations of related records.

  5. $migrationDependancy Property: This property lists the table names that are needed in this model during migration. It indicates dependencies on other database tables, ensuring they exist before running migrations for this model.

  6. $table Property: This property specifies the name of the database table associated with the Invoice model. In this case, the table name is "account_invoice".

  7. $show_frontend Property: This property indicates whether the model is visible from the frontend. It is set to true, suggesting that instances of this model can be displayed in frontend views.

  8. fields() Method: This method defines the database schema fields for the Invoice model. It utilizes Laravel's Schema Blueprint to define the fields, their types, defaults, and constraints.

  9. structure() Method: This method defines the structure for the Invoice model, which includes the table columns, form layout, and filtering criteria. This structure might be used for generating UI components or configuring data grids.

Overall, this Invoice model appears to handle invoicing-related data within the "Account" module of your Laravel application. It provides functionality for defining database schema, rendering relationships, and configuring UI representations of invoice data.

Last updated