Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone
We have now created  our database table, next step is to create a data model to represent it. Laravel provides excellent ORM (object-relational mapper) known as ‘Eloquent’ for that, so we don’t need to write sql statements and direct database access using connections, commands for simple CRUD operations. But I personally like to use stored procedures when it comes to complex data loading as I have seen lot of ORM mappers fail in generating optimized queries or at least it will send multiple Database requests to load required dataset. But for this tutorial we can stick to ORM.

Even though this is simple application I normally like to do my design in such way that it will grow in future so we can re-use as many logic as possible. Sometimes I realize this at the beginning or sometime I realize after doing some implementation. As this is not pre-written set of tutorials, you may find I will do some changes later as I discover them. (ex: if you remember that we changed VirtualHost entry pointing to ‘public’ folder in previous post)

Step 1 – Implementing BaseModel

A sip of Coffee with Laravel and Backbone - Book

 

I think we need some base model class so that we can put common logic for any model that created to represent a table, into this. Create new php file in applications/models folder, and give the name basemodel.php Open it in editor and add following code.

Here we have a base model that we can use to derive all other models, and if you need to use Laravel ORM features, you need the model extended from Eloquent class, and here I have done it for my base model class, so all other models will automatically extended from Eloquent as well. For now we will keep this BaseModel class empty and will add any logic to this as we find something common to all models.

Step 2 – Creating Model to Represent Category Table

Next we will create model to represent tbl_category table that we created in previous post. In the same folder as above create a new file called category.php and open it in your editor, write following code.

By default you don’t need to define $table static variable here if you use the plural form for the database table of your model. But I like to keep my table names and class names in my control, so that I can later change whichever without causing any issue on the other and all related entities of them.

I think that’s it for now, but I will touch these models again when we implement controllers and doing real data loading or saving.

Posts of this series
Laravel, Backbone.js + Coffeescript Tutorial Part I – Introduction
Laravel, Backbone.js + Coffeescript Tutorial Part II – Setup
Laravel, Backbone.js + Coffeescript Tutorial Part III – Install Laravel
Laravel, Backbone.js + Coffeescript Tutorial Part IV – Database
Laravel, Backbone.js + Coffeescript Tutorial Part V – Data model
Laravel, Backbone.js + Coffeescript Tutorial Part VI – REST API
Laravel, Backbone.js + Coffeescript Tutorial Part VII – REST API
Laravel, Backbone.js + Coffeescript Tutorial Part VIII – Views
Laravel, Backbone.js + Coffeescript Tutorial Part IX – Coffeescript
Laravel, Backbone.js + Coffeescript Tutorial Part X – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XI – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XII – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XIII – Backbone

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone