0

I have worked with some Laravel projects in the past. Creating a table and using them in a controller is pretty easy. However, I mostly work with pure PHP. Most of them are not class object based. I want to write code to work similar to the Laravel model system. I think it will be helpful to generate a DB table by hand.

I need to achieve the following:

Create a table manually and make class and the same name as the table. The class should able to get all the table info (fields, id, etc.) using the class name when creating the object (not assigning table name).

The class should able to get table fields dynamically and must have the ability to save data using Object->save(), and get a row using id: $obj=Object::find(2);

class drivers extends Model
{
//'''''
}

$driver = new $driver();
$diver->name="foo";
$diver->age=19;
$diver->save();
//------------
$diver=Driver::find(3);
$diver->delete();

I want to do the above in in pure PHP but I am unable to manage. Can anyone help? I really appreciate it, thank you.

I tried it like this:

<?php

class  Model
{

   protected function __construct() {

    public static function find($id){
        $tablename=get_somehow_extented_classname;
        //....

        return $datarow;
    }

    public static function delete($id){
        $tablename=get_somehow_extented_classname;
        //....
        $result=query("delete from $tablename where id=$id");

        return $result;
    }
}

use Model; // core model (I want this)

class drivers extends Model
{
    // nothing lot of things here
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Mobile Dev
  • 49
  • 1
  • 9
  • What have you tried so far? What you are asking cannot be fixed in just a few lines of code, you are looking for an orm. – Jerodev Dec 04 '18 at 12:01
  • Google for "eloquent without laravel" to check how others achieved it. – Tpojka Dec 04 '18 at 12:47
  • Just don't... especially if you're not going to use prepared statements. – Devon Bessemer Dec 04 '18 at 12:59
  • There's [Torch](https://github.com/mattstauffer/Torch): "_Examples of using each Illuminate component in non-Laravel applications_" Maybe this can help a bit. – brombeer Dec 04 '18 at 13:12

1 Answers1

0

found solution for my problem -> https://medium.com/@kshitij206/use-eloquent-without-laravel-7e1c73d79977 thanks..

here i did steps..(sometimes can be helpful for other) created folder in xammp(my php running environment)

executed composer require illuminate/database

created bootstrap.php at root

<?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" => "testdatabase",

   "username" => "root",

   "password" => ""

]);

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

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

$capsule->bootEloquent();

created admins.php at root

<?php
require "vendor/autoload.php";
use Illuminate\Database\Eloquent\Model;

class admins extends Model
{

          public $timestamps = false;
          protected $primaryKey = 'admin_id';//primery key defaut id

}

created test.php at root

<?php

require "bootstrap.php";
require "admins.php";


$admin = new admins;
$admin->admin_email="sdfdfd@.sds";
$admin->admin_password="ljkklj";
$admin->username="sdfdfd";
$admin->profile_img ="pic\340943.png";
$admin->save();

//....
$admin2=admins::find(2);

and it is working... im gonna use this on my future pure php projects :)

Mobile Dev
  • 49
  • 1
  • 9