Home » Php » How to create Laravel Model without Eloquent?

How to create Laravel Model without Eloquent?

Posted by: admin October 29, 2017 Leave a comment

Questions:

As I am not sure, is it possible to create models with DB Class instead of Eloquent? I want to stay away from ORM.

Thanks

Answers:

Yes of course its possible. You dont need to extend any class to make a model class that encapsulates business logic and consists of methods calling the DB class.

Just create your model inside app/models/MyModel.php like this

class MyModel{

    public static function getMyData(){
         DB::table('users')->select('column')->get();

    }
}

then you should be fine to call your new class statically:

$data = MyModel::getMyData();

If you wanted to extend the DB class you could, though more likely you would be looking to extend the Database/Builder class to extend functionality but this is a complex topic and I suspect you would have asked a very different question if this was what you were after.

As I final note, I wouldn’t steer clear of Eloquent, it’s the greatest thing about Laravel amongst a lot of other great things

Questions:
Answers:

Just remove the “extends Eloquent” and build the queries using the DB class.

Questions:
Answers:

Goofy,

You should make your function static.

Eg,

class Type {
    public static function type_get() {
        DB::table('story_types')->get();
    }
}