Use Eloquent without Laravel

Kshitij Soni
Dec 11, 2017 · 4 min read
composer require illuminate/database
<?php



require "vendor/autoload.php";

use Illuminate\Database\Capsule\Manager as Capsule;



$capsule = new Capsule;



$capsule->addConnection([

"driver" => "mysql",

"host" =>"127.0.0.1",

"database" => "acl",

"username" => "root",

"password" => ""

]);

//Make this Capsule instance available globally.
$capsule->setAsGlobal();

// Setup the Eloquent ORM.
$capsule->bootEloquent();
$capsule->bootEloquent();
<?php

require "../bootstrap.php";



use Illuminate\Database\Capsule\Manager as Capsule;



Capsule::schema()->create('users', function ($table) {

$table->increments('id');

$table->string('name');

$table->string('email')->unique();

$table->string('password');

$table->string('userimage')->nullable();

$table->string('api_key')->nullable()->unique();

$table->rememberToken();

$table->timestamps();

});
<?php

require "../bootstrap.php";



use Illuminate\Database\Capsule\Manager as Capsule;



Capsule::schema()->create('todos', function ($table) {

$table->increments('id');

$table->string('todo');

$table->string('description');

$table->string('category');

$table->integer('user_id')->unsigned();

$table->timestamps();

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

});
"autoload": {

"classmap": [

"classes"

]

}
<?php

use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Eloquent

{

/**
* The attributes that are mass assignable.
*
* @var array
*/

protected $fillable = [

'name', 'email', 'password','userimage'

];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/

protected $hidden = [

'password', 'remember_token',

];

/*
* Get Todo of User
*
*/

public function todo()

{
return $this->hasMany('Todo');

}

}
<?php



use Illuminate\Database\Eloquent\Model as Eloquent;



class Todo extends Eloquent

{

protected $fillable = ['todo','category','description'];

}
<?php

require "bootstrap.php";

$user = User::Create([ 'name' => "Kshiitj Soni", 'email' => "kshitij206@gmail.com", 'password' => password_hash("1234",PASSWORD_BCRYPT), ]);

print_r($user->todo()->create([

'todo' => "Working with Eloquent Without PHP",

'category' => "eloquent",

'description' => "Testing the work using eloquent without laravel"

]));

Kshitij Soni

Written by

Developer by profession . Youtube channel : https://www.youtube.com/channel/UCOWT2JvRnSMVSboxvccZBFQ

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade