I am developing a project in core PHP and I want to use eloquent query structure in my project to make ease of setting up mySQL connections and executing mySQL queries .
2 Answers
Start by using
composer require illuminate/database vlucas/phpdotenv
create a bootstrap file to bootstrap
Eloquent
's connection string://bootstrap.php <?php require 'vendor/autoload.php'; use Illuminate\Database\Capsule\Manager as Capsule; $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); $capsule = new Capsule; $capsule->addConnection([ 'driver' => env('DB_CONNECTION'), 'host' => env('DB_HOST'), 'port' => env('DB_PORT'), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ]); $capsule->setAsGlobal(); $capsule->bootEloquent();
Add environment variables!! (.env file)
Create a model file, you can put this anywhere you want
//Models/User.php <?php namespace Models; use Illuminate\Database\Eloquent\Model; class User extends Model { }
Use them!
<?php require('bootstrap.php'); use Models\User; use Illuminate\Database\Connection as DB; $user = User::find(1); $user2 = User::where('name', 'somename')->first();
You can use opensource project http://www.phptomb.com You can customise the existing class to meet your requirements.
-
-
Because eloquent ask to install whole bunch of libraries and phptomb is just a single class file. Easy to use, easy to customise, For beginners especially. Nov 22, 2018 at 7:42
-
-
If someone with core php know composer and elequant file, he would be using laravel. But a fresher with no framework knowledge and oops depth knowledge can use phptomb. Really admire answer of joe, right way to adopt elequant if you know a little of laravel framework and composer. Nov 22, 2018 at 8:11
Torch
repository: github.com/mattstauffer/Torch It's not really up-to-date yet but yet could give some valuable insight to do it on your own.