Hackernoon logoRails and Laravel Explained With Basic MVC Structure by@men32z

Rails and Laravel Explained With Basic MVC Structure

Author profile picture

@men32zLuis Preza

Ok, first of all, this isn’t an article about which framework is better since they have different base languages.
If you are a beginner this post will help to understand frameworks and how they work. If you already use one of them you will see that they are
pretty similar.
I have worked with Laravel before but recently I got into Rails and found out that they are actually very similar frameworks. so let’s start with a little background.
A Framework is a bunch of structures, rules, and concepts that help the programmers to work in a team without going crazy. These two frameworks will help you to create websites in an easier and faster way than do it with vanilla code.
Rails is a Ruby framework created by David Heinemeier and released in 2005.
Laravel is a PHP framework created by Taylor Otwell and released in 2011. both use the MVC pattern, both have continuous support and a huge community.
But let’s start with the MVC so we can see how they work. the first thing
is the ‘M’ the Model, this is how the frameworks communicate with the
database through an ORM. Rails use ActiveRecord and Laravel uses
Eloquent.
you can create a model with this command in Rails.
rails generate model Car
The same comand in Laravel
php artisan make:model Car -m
This command is going to generate 2 files, The model and the migration, but I’m not going to talk about that because this is not a Laravel or Rails
tutorial. the structure of the folders it’s pretty much the same, so you
can find your model and migrations easily.
A basic migration code its the following.
// Laravel /database/migrations/date_code_create_cars_table.php
public function up()
    {
        Schema::create('cars', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name'); // not generated
            $table->integer('year'); // not generated
            $table->timestamps();
        });
    }
public function down()
    {
        Schema::dropIfExists('cars');
    }
# Rails /db/migrate/date_code_create_cars.rb
def change
    create_table :comments do |t|
      t.string :name # not generated
      t.integer :year # not generated
      t.timestamps
    end
  end
The migrations are the same but you can see how Rails do the work for you by doing ‘up’ and ‘down’ in one method ‘change’. we added 2 lines in
each migration and they have similar syntax. but we could easily do this
in rails extending the previous command like this.
rails g model Car name:string year:integer
And this is just one example because rails could do so much more like the
entire CRUD scaffold for a model.
We are going to insert one car in the databases so we can work with it without doing all the CRUD.
INSERT INTO cars(name, year) VALUES('Tesla', 2019)
So let’s continue with the ‘C’ that includes the Route and the Controller. first, we are going to set the routes that look like this.
# Rails /config/routes.rb
get ‘/car’, to: ‘car#index’
// Laravel /routes/web.php
Route::get(‘/car’, ‘CarController@index’)->name(‘car.index’);
As you can see its pretty much the same, rails automatically name the
route so you can use it in the view and with Laravel, you have to name
it. Although Rails syntax looks simpler could be magical or mystical, and sometimes we want to know exactly what we are doing.
Then we have the controller, we could generate the controller with the basic commands.
Laravel
php artisan make:controller CarController
Rails
rails g controller car
Rails is going to generate also a folder for the controller views in
‘/app/views’
, with Laravel you have to manually create it. and this is
how the controllers look like.
# Rails /app/controllers/car_controller.rb
def index
   @car = Car.first
end
// Laravel /app/Http/Controllers/CarController.php
use App\Car;
.
.
public function index()
{
   $car = Car::first();
   return view("car.index", compact('car'));
}
Again we see how rails magically returns the view while in Laravel we have to specify it. And also you have to import the class “Car” if you want to
use it.
Finally we have to create our views, we can just create the files. Laravel uses “Blade” as a Template Engine, and Rails uses “ERB” so the code looks like this.
# Rails /app/views/car/index.html.erb
<b>Car name:</b>
<%= @car.name %>
<br>
<b>Car year:</b>
<%= @car.year %>
// Laravel /resources/views/car/index.blade.php
@extends('layout.app')
@section('content')
  <b>Car name:</b>
  {{$car->name}}
  <br>
  <b>Car year:</b>
  {{$car->year}}
@endsection
You can create the layout with Laravel and use it like in the previous
example and Rails is going to create it for you. also, you don’t have to specify the layout in Rails(but you can).
And finally, you have something like this

Conclusion

I have a lot of respect for Laravel, and I love it, but we can see how
Rails makes all the basics really easy. Both are great frameworks and
you need to have in mind that there a lot of factors to consider to use
one of them, like deploy, hosting costs, performance, etc..
But why just learn one? once you learn one the second is a piece of cake. so don’t let the “code wars” blind you, and learn whatever you want in the
end knowledge is always good.
Rails❤Laravel.

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!