2

I'm new at MVC and my first framework is Laravel (3 for now). I've started coding exclusively in the routes, and I moved to the controller. I'm however doing all of my database operations in the controller. I do not understand how to use the model.

Examples either demonstrate everything in the controller or in the route, but they never split the model, controller and view.

Could anyone kindly explain me how to use the model? In short I don't understand how to link one to each other, like sending form input to them model, or processed data back to the controller.

A github repo of a Laravel (v3 if possible) with a full MVC setup would be nice to analyze too, if anyone has one up for me to look at?

Thanks.

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
veksen
  • 6,863
  • 5
  • 20
  • 33
  • Why? Laravel is not even implementing MVC or anything remotely MVC-inspired. What you have is just a collection of instances that look vaguely like active record. – tereško Sep 12 '13 at 20:26
  • 1
    Just a thought: if you are going to start learning Laravel, you are better off starting with Laravel 4. – raeq Sep 12 '13 at 20:54
  • @tereško what do you suggest that implements MVC then, that is not Zend? Also, why the downvote? – veksen Sep 12 '13 at 21:02
  • 1
    Frameworks do not implement MVC. Your code does. Or doesn't. – tereško Sep 12 '13 at 21:04
  • A model is a class that describes a database table. An instance of a model is a row in that database table. Models are really only data-containers. – Populus Sep 17 '13 at 15:02
  • Model is your silent hard worker. Whenever you need something calculated and want to use the returned result in the controller or passing to your view, you write that function in your model and simply call it everywhere else. – JustinHo Feb 06 '14 at 00:10

2 Answers2

4

The best statement on the subject of Frameworks I've heard is due to Uncle Bob:

A good Architecture allows major decisions to be deferred!

Specifically:

  • A good Architecture delays choosing a Framework!

Another great piece to think about:

MVC is not an Architecture! It is a Delivery Design Pattern.

Watch his video - it is one of the sadly few ones out there that don't spend 1000 words on what can be said in 10 and I can't highly enough recommend it - and it will help you to understand many points raised in your question:

Robert C Martin(Uncle Bob) -Clean Architecture and Design - Video

Of course, his book on Clean Code is also highly recommended!

Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
  • Good recommendation! This video (I saw it nearly 1.6 years ago) just changed my vision, the image on my brain I drew about software development and opened another door of learning and everything is so clear now. Thanks uncle Bob :-) – The Alpha Jun 15 '14 at 21:27
0

Although this link is for Laravel 4 docs, it may help you understand how the models work - (Laravel 3 also uses Eloquent):

http://laravel.com/docs/eloquent

Also, specific to laravel 3:

http://codehappy.daylerees.com/eloquent-orm

Ryan
  • 5,959
  • 2
  • 25
  • 24
  • what ORM has got to do with MVC? – itachi Oct 23 '13 at 19:02
  • @itachi, OP asked about lack of talk about "models" in whatever examples he's been looking at, specifically in Laravel 3. As Populus mentions in his comment, "An instance of a model is a row in that database table." ORM = Object Relational Mapper; therefore, the ORM maps the Object to the Database, right? Can you use Laravel without Eloquent? Yes, but Model Objects in Laravel should typically EXTEND Eloquent. Search google for "Laravel Models", and everything you see discusses eloquent. Please actually read the links I provided - the pages are titled "Eloquent ORM" but they are ABOUT models. – Ryan Oct 23 '13 at 20:17
  • point is models != eloquent. Eloquent is an ORM. ORM is just a part of a model, NOT the whole model. – itachi Oct 24 '13 at 05:15
  • Model != ORM, that's fine. I'll rephrase my point: in a fresh, out-of-the-box Laravel install, Models can and should always EXTEND the Eloquent class, enabling you to use the ORM. OP asked: "how to use the model" in laravel. Eloquent is an integral feature of the Laravel framework. In my opinion, if you're trying to understand how models work in Laravel, seek to understand Eloquent. – Ryan Oct 24 '13 at 14:05