2Answers
  • 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Laravel 4 Models, how to use them

I've been looking at Laravel for awhile and I decided to finally pick it up. It's my first time using a PHP framework and I'm having some trouble grasping the purpose of the models.

I've been reading a lot of newbie guides and this is pretty much all that is in their model (Laravel wise),

class Model extends Eloquent {

}

And then in their controller they do something like this,

$model = new Model;
$model->text = "text";
$model->save();

I'm no expert at the MVC pattern (probably the biggest newbie out there) but I thought the whole point (or at least a small point) was to separate a lot of actions. And that the model was supposed to be responsible of handling everything DB-wise. So in a way, this seems wrong to me or at least not the best practice.

But if you start setting up a bunch of functions you could run into the issue of having one model for every table. Which again, doesn't seem right. So you have to make the model, in a way, ambiguous. In the sense that it can take any actions against any table?

It all just seems really confusing to me at the moment.

You are going to need a model for evey table, because there are other things related to models that cannot be shared, like column names and validation, but if you think you are repeating yourself, you can create a BaseModel and add all methods or even overload Eloquent methods in it:

class BaseModel extends Eloquent {

    public function whatever() {

    }

    public function save(array $options = []) {
        // do what you need to do
        parent::save();
    }

}

Then create your models using it:

class Order extends BaseModel {

}

But you don't need much on a model, if your models and tables names follows Laravel pattern (in this case the table name would be 'orders'), then you just need this simple declaration to have a model working for your table.

Edit:

Controllers are meant to transfer data from a model to a view, but they should not know too much about your data, almost everything about them should lies in your models ("Fat models, skinny controllers"), so they need to know just enough to have "control".

class OrdersController extends BaseController {

    public function process()
    {
        $order = Order::find( Input::get('orderId') )->process();

        return View::make('orders.showProcessedOrder')->with('order',$order);
    }

}
  • 14
Reply Report
      • 2
    • Great answer! And to elaborate a bit on what I initially meant, if I want to grab a row with the id of 1 I could do Model::find(1); in my controller but would it be best practice (loosely using that term) to place that "find" in a function and return the result to the controller instead of actually doing it in the controller? In this case it might not but if think bigger things.
    • But you're actually right. They forgot about data sources and adapters. That's why you might think things like validation and relationships must be rewritten a million times over for each database. Not true. At all. ...And this magical nirvana is even possible with Laravel, but you gotta wire all that up yourself. Is it worth it? Or will you subscribe to the disorganization? Up to you, but I think your original question was good. Whether you consider yourself a newbie or not, you're thinking about good design practice.

Models are a wrapper around your database tables. Your database tables are the real-world things you are making your application about. Models allow you to use your programming language for CRUD'ing (creating, reading, updating or deleting) data. OOP is all about classes and objects, and converting things like incoming HTTP requests and data storage into that form.

Your question is a good one. When you learn how to make Web applications, having a three-tiered web app - a presentation layer, a business-logic layer and data-storage layer with data stored in a relational database - works great, and it makes no doggone sense to add an extra layer of database-related stuff in the code.

And, like Antonio wrote, in MVC programming, "fat models, skinny controllers" is what you're working towards. The controller ideally should just be a couple of lines of code that pass the incoming request to the correct model where it could be validated, added to the database, etc. (But it is easiest to put that in the controller while you're first learning/figuring MVC out.)

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions