Laravel 3: Looking for explanation how to use the model
Asked Answered
T

2

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?

Tervalent answered 12/9, 2013 at 20:18 Comment(4)
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.Catalpa
Frameworks do not implement MVC. Your code does. Or doesn't.Catalpa
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.Carlin
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.Runofthemill
C
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!

Cicily answered 17/9, 2013 at 14:57 Comment(0)
A
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

Alarmist answered 18/10, 2013 at 3:45 Comment(4)
what ORM has got to do with MVC?Disaffirm
@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.Alarmist
point is models != eloquent. Eloquent is an ORM. ORM is just a part of a model, NOT the whole model.Disaffirm
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.Alarmist

© 2022 - 2024 — McMap. All rights reserved.