Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

CodeWall:
Use Laravel Eloquent Query Builder In Any PHP Project
Sep 04, 2018 @ 10:30:49

On the CodeWall.co.uk site there's a new tutorial showing you how to use Eloquent outside of Laravel applications thanks to its "capsule" functionality. Eloquent is an ORM layer that's a part of the Laravel framework and makes it easier to work with records and sets of data from your database.

OWASP (Open Web Application Security Project) is a project that notes down the current threats to a web application. And I have been researching on their site and I have found this similarity in their 2010, 2013 and 2017 report that, SQL Injection or any other type of Injection is number 1 on this list, every time.

And that’s a part to worry.

This can cause you to get out of business, so this is pretty serious and your organisation should take care of the this issues and prevent yourself from it.

The tutorial starts with a brief introduction of what SQL injection is including some example SQL to show how the injection happens. It then covers how to prevent this issue with base PHP code (no framework or package) using prepared statements and bound parameters. Eloquent takes care of this for you and provides a lot of other handy features. The article goes on to show:

  • how to install the Eloquent packages with Composer
  • the code to create the "capsule" that's used as a bridge into the Eloquent code
  • the creation of migrations for two tables: users and posts
  • how to make models for these two tables

The post wraps up with a look at using these models to create a new user and post record using the ORM interface rather than manual SQL statements.

tagged: tutorial laravel eloquent query orm introduction model capsule

Link: https://www.codewall.co.uk/use-laravel-eloquent-query-builder-in-any-php-project/

TutsPlus.com:
Using Illuminate Database With Eloquent in Your PHP App Without Laravel
Jan 26, 2017 @ 09:51:05

The TutsPlus.com site has posted a new tutorial showing you how you can use the Eloquent ORM from Laravel without having to use Laravel itself. There's already functionality built into the popular database interface that makes it possible, it's just a matter of turning it on.

Illuminate is Laravel’s database engine minus Laravel. It comes bundled with the Eloquent ORM in Laravel. If you would like to build your PHP apps with ORMs and prefer not to use Laravel, this tutorial is for you.

In this tutorial, we are going to build the back end for a Q&A App with PHP, Illuminate Database, and the Eloquent ORM.

They start the post with a list of requirements and a brief outline of what functionality the end result will include. With that defined the tutorial starts in on the directory structure of the application and the Composer configuration to pull in Eloquent and its requirements. Next up is the code to define the connection credentials and (raw) SQL to create the tables in the database. The database "capsule" is created and the code is shared to create simple controllers and matching models for users, questions, answers and upvotes. They show how to use relationships to get the linked objects between tables and how to perform update and delete operations.

tagged: illuminate database eloquent laravel outside tutorial capsule

Link: https://code.tutsplus.com/tutorials/using-illuminate-database-with-eloquent-in-your-php-app-without-laravel--cms-27247

Rob Allen:
Using Eloquent in Slim Framework
Apr 07, 2016 @ 09:45:31

Rob Allen has a quick post showing how to use Eloquent in a Slim framework application to work with your database queries and functionality.

Eloquent is one of the easier ORMs to get started with, so let's look at how to use and test it within a Slim Framework application. [...] Eloquent is quite a nice implementation of the Active Record pattern. If that meets your needs, then it is easy to use within a Slim Framework application, so go for it!

He walks you through the set up of the Eloquent package via a Composer require and update your Slim configuration with the database settings. He then includes the few lines you'll need to use the "capsule" manager to integrate it with the Slim structure. He also talks about testing models and shows how to create some simple tests with PHPUnit and a bit of mocking.

tagged: slimframework eloquent database capsule composer tutorial

Link: https://akrabat.com/using-testing-eloquent-in-slim-framework/