Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 707
Andreas Avatar asked Jul 27 '13 11:07

Andreas


People also ask

How do Laravel models work?

Laravel Create Model is an MVC based PHP system. In the MVC architecture, 'M' stands for 'Model'. A model is used as a way for questioning data to and from the table within the database. Laravel gives a basic way to do that using Eloquent ORM where each table incorporates a Model to interact with it.

What is $fillable in Laravel?

In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment. Mass assignment refers to sending an array to the model to directly create a new record in Database.

What are models for in Laravel?

In Laravel, Model is a class that represents the logical structure and relationship of underlying data table. In Laravel, each of the database table has a corresponding “Model” that allow us to interact with that table. Models gives you the way to retrieve, insert, and update information into your data table.


1 Answers

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);
    }

}
like image 51
Antonio Carlos Ribeiro Avatar answered Oct 21 '22 04:10

Antonio Carlos Ribeiro