visit
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.
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.
rails generate model Car
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.
// 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.
INSERT INTO cars(name, year) VALUES('Tesla', 2019)
# 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.
php artisan make:controller CarController
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 # 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.
# 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).
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.