6.5.1. A Tool for Model-making

To create a model for our customer entity, Laravel offers the artisan command that makes it relatively easy. This is the command for creating a model template:

  1. php artisan make:model Customer

We want to change the model so that it looks like this:

  1. namespace App;
  2. use Firebird\Eloquent\Model;
  3. class Customer extends Model
  4. {
  5. /**
  6. * Table associated with the model
  7. *
  8. * @var string
  9. */
  10. protected $table = 'CUSTOMER';
  11. /**
  12. * Primary key of the model
  13. *
  14. * @var string
  15. */
  16. protected $primaryKey = 'CUSTOMER_ID';
  17. /**
  18. * Our model does not have a timestamp
  19. *
  20. * @var bool
  21. */
  22. public $timestamps = false;
  23. /**
  24. * The name of the sequence for generating the primary key
  25. *
  26. * @var string
  27. */
  28. protected $sequence = 'GEN_CUSTOMER_ID';
  29. }

Notice that we use the modified Firebird\Eloquent\Model model from the sim1984/laravel-firebird package as the basis. It allows us to use the sequence specified in the $sequence attribute to generate values for the primary key ID.

We create a model for products — Product — in the same way.

  1. namespace App;
  2. use Firebird\Eloquent\Model;
  3. class Product extends Model
  4. {
  5. /**
  6. * Table associated with the model
  7. *
  8. * @var string
  9. */
  10. protected $table = 'PRODUCT';
  11. /**
  12. * Primary key of the model
  13. *
  14. * @var string
  15. */
  16. protected $primaryKey = 'PRODUCT_ID';
  17. /**
  18. * Our model does not have a timestamp
  19. *
  20. * @var bool
  21. */
  22. public $timestamps = false;
  23. /**
  24. * The name of the sequence for generating the primary key
  25. *
  26. * @var string
  27. */
  28. protected $sequence = 'GEN_PRODUCT_ID';
  29. }

Now, a model for the invoice header:

  1. namespace App;
  2. use Firebird\Eloquent\Model;
  3. class Invoice extends Model {
  4. /**
  5. * Table associated with the model
  6. *
  7. * @var string
  8. */
  9. protected $table = 'INVOICE';
  10. /**
  11. * Primary key of the model
  12. *
  13. * @var string
  14. */
  15. protected $primaryKey = 'INVOICE_ID';
  16. /**
  17. * Our model does not have a timestamp
  18. *
  19. * @var bool
  20. */
  21. public $timestamps = false;
  22. /**
  23. * The name of the sequence for generating the primary key
  24. *
  25. * @var string
  26. */
  27. protected $sequence = 'GEN_INVOICE_ID';
  28. /**
  29. * Customer
  30. *
  31. * @return \App\Customer
  32. */
  33. public function customer() {
  34. return $this->belongsTo('App\Customer', 'CUSTOMER_ID');
  35. }
  36. /**
  37. * Invoice lines
  38. * @return \App\InvoiceLine[]
  39. */
  40. public function lines() {
  41. return $this->hasMany('App\InvoiceLine', 'INVOICE_ID');
  42. }
  43. /**
  44. * Payed
  45. */
  46. public function pay() {
  47. $connection = $this->getConnection();
  48. $attributes = $this->attributes;
  49. $connection->executeProcedure('SP_PAY_FOR_INOVICE',
  50. [$attributes['INVOICE_ID']]);
  51. }
  52. }

You’ll observe some additional functions in this model. The customer function returns the customer that relates to the invoice header via the CUSTOMER_ID field. The belongsTo method is used for establishing this relation. The name of the model class and the name of the relation field are passed to this method.

The function lines returns items from the invoice that are represented by a collection of InvoiceLine models, described later. To establish the one-to-many relation in the lines function, the name of the class model and the relation field are passed to the hasMany method.

You can find more details about specifying relations between entities in the Relationships section of the Laravel documentation.

The pay function performs payment of an invoice by calling the stored procedure SP_PAY_FOR_INVOICE, passing the identifier of the invoice header. The value of any field (model attribute) can be obtained from the attribute attribute. The executeProcedure method calls the stored procedure.

This method is available only when the sim1984/laravel-firebird extension is used.