Web Programming TutorialsLearn about Models in Laravel

Learn about Models in Laravel

Models in Laravel_bigToday we will learn about Models in Laravel in this tutorial.

Model-View-Controller (MVC) is a very common design pattern in modern web applications. Many of the popular frameworks are built around this architecture. Laravel is also one of these.
Today we are talking about Model part of this MVC architecture.

  • What are Models?
    • Models are representatives of the Database, and it should contain all the business logic of an application.
    • Controllers communicate with Models and ask them to retrieve information they need.
    • This information is then passed by a Controller to the View and is used.
    • It’s very rare that a Model directly interacts with a View, but sometimes it may happen when necessary.
    • Models can talk with other Models. Models can even have relationships with other models.
    • These relationships make it easier and quicker for a Controller to get information, since it doesn’t have to interact with different Models – the Models can do that themselves.
    • Laravel gives us an easy way to built models by providing us with different general purpose methods that most models may need. It is called the Eloquent ORM.
    • An ORM is an Object Relational Mapper and Eloquent ORM is Laravel’s built-in ORM implementation.
  • Some conventions followed by Eloquent ORM:
    • If a table which is represented by the model contains an id field and it is a primary key. It will be used by most of the eloquent methods.
    • Next the Eloquents considers that your table name is the plural form of your model name. For example, the model with name user will represent users table.
    • But every time this is not satisfied i.e. we might have given another name to our table. In this situation it also provides us the way to specify the table name. This way is to use the $table flag.
    • For example,
    • class User extends Eloquent {
          public static $table = 'new_users';
    • This will instruct laravel to consider the table name as new_users instead of convention table name users.
  • Retrieving records:
    • There are different methods used to retrieve records based on our selection criterion.
      1. If you want to search for a record with a particular ID, you can use the following statement
      2. $player = Player::find($player_id);
      3. If you want to retrieve a players information from his phone number, you can get it by using the following statement,
      4. $player = Player::where('phone', '=', $phone)->first();
  • Inserting and Updating records:
    • You can insert and update models using eloquent by using 3 steps:
      1. Create a model
      2. $player = new Player();
        //or get an existing player
        $player = Player::get($player_id);
      3. Set the data
      4. $player->email = '[email protected]’;
        $player->password = 'Cap12345';
      5. Save the record
      6. $user->save();
  • Defining relationships:
    • Eloquent makes the process of defining relationships and retrieving related models very simple.
    • It supports 3 types of relationships: One-to-One, One-to-Many and Many-to-Many.

Thus we studied some important things of Models in this Models in Laravel tutorial.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -