Learning the Laravel framework is something I have wanted to do for some time now. Here is what I found easy with Laravel from my understanding after just 1 week of learning.
Foreword
I did start off with a solid understanding and experience with vanilla PHP, MySQL and Object orientated framework (OOP) usage.
Laravel is based on the model view controller (MVC) framework, this was a new concept to me and wrapping my head around the model, view and controller has been the hardest part.
I watched Laracasts Laravel 8 from scratch video series up to section 5. This provided a good rundown on MVC, Routes, Views, Blade and Migrations/Eloquent (DB).
Another handy asset was using PhpStorm IDE, which recommends and gives hints on everything that gets written. I have used PhpStorm for years but with all the namespaces and “foreign” functions, it was invaluable.
After a week of watching, reading and trialling here is what I have found Laravel makes easy in development.
Routes
No longer do you need directories or .htacess
magic, Request methods and locations are written out with ease.
Return a function, class or view for routes
Route::get('/about/faq', function () { return view('about.faq'); })
The routing used in Laravel actually comes from Symfony another very popular framework.
As per the below authentication is easily added on to routes.
Authentication
At a base level Laravel provides self-proclaimed “guards” Laravel also provides a starter kit for Authentication called Breeze. This starter kit is a great way to dissect the methods and see the standard for laying out app authentication.
Validation
Using Laravel saves you from needing to write custom validation functions and rules to validate all incoming data.
Look at all the validation rules here, there are many handy ones.
Blade templates
Blade is the templating engine for Laravel and it is much better than using PHP shortcodes mixed in with HTML.
The if condition and loops are very readable
Displaying variables seems to stand out more…
<h1 class='text-primary'>{{$user->name}}</h1>
Components are also possible, this cuts down on repeat code and makes views (pages) easier to edit and dissect.
Middleware
Middleware is an implementation method for authentication, it provides an easy way to filter traffic at your application. Things like ensuring a token is provided or even throttling.
Database migrations & seeding
With Laravel Migrations you can write out the MySQL table structure as PHP code.
Schema::create('users', function (Blueprint $table) { $table->id()->autoIncrement(); $table->string('first'); $table->string('last'); $table->string('email'); $table->boolean('gender'); $table->integer('height'); $table->integer('weight'); $table->date()('dob'); $table->timestamps(); });
A different concept at first but this does save you from running SQL time after time. It also serves as version control.
There is also seeding so you can fill your database with data, again written in PHP rather than a SQL INSERT statement/file.
Database CRUD
An extension from above inserting, selecting, deleting and updating MySQL data with GET and POST requests is just so simple.
This is called a resource controller.
No more raw PDO SQL queries where everything has to be defined.
This returns all data from the users table:
public function index() { $users = User::all(); }
This will validate and insert data into the “server” table from a POST form:
public function store(Request $request) { $request->validate([ 'hostname' => 'required', 'location_id' => 'numeric', 'price' => 'numeric' ]); Server::create([ 'hostname' => $request->hostname, 'location_id' => $request->location_id, 'price' => $request->price, ]); }
Documentation
The documentation for Laravel is extremely deep and well written on everything that comes with Laravel. Between the official documentation and Laracasts there is plenty to build upon an understanding of the framework.