19

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

Volatil3
  • 14,253
  • 38
  • 134
  • 263

2 Answers2

22

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(){
         return 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

Dev Matee
  • 5,525
  • 2
  • 27
  • 33
SwiftD
  • 5,769
  • 6
  • 43
  • 67
  • 3
    I always run away from ORM/unnecessary addition of layers in an application design. It might make a developer happy but not a user. Thanks for your answer! – Volatil3 Jan 14 '14 at 18:50
  • 2
    @Votail3 each to their own but I find performance comparable and less code means less bugs and more time for devs to create new features to keep those users happy, combined with less bugs I mentioned my users are now positively joyous and so are my devs. Eloquent makes the world a happier place but I do appreciate their are situations where I might drop it. Great thing is you can easily mix approaches, perhaps with a focus on bottleneck query optimization. – SwiftD Jan 14 '14 at 23:49
  • it says: * MyModel Class Not found* When I try to call in Controller – Volatil3 Jan 15 '14 at 06:15
  • It shouldn't if MyModel is directly in your Models folder, but if it is in a sub-folder or somewhere else you will need to run ``php artisan dump-autoload`` from the command line – SwiftD Jan 15 '14 at 11:06
  • It adds an *entry* of new model in autoload_map.php file. Either one does manually or run autoload dump command – Volatil3 Jan 15 '14 at 18:18
  • You'll have to do some googling to check me on this but I believe dump-autoload does some pre-loading of your classes but it does take a while, I think you can also add an entry to the compile.php config, add directories in your global.php start file or in your composer.json – SwiftD Jan 15 '14 at 23:55
  • Should not Model,Controller and Views already in path? I don't get why it does not sense new file in model Folder. – Volatil3 Jan 16 '14 at 05:51
  • Like I say, it should do. As long as your model is in the route of your models directory and is named the same as the class, so class MyModel is in a file called MyModel.php – SwiftD Jan 16 '14 at 12:30
  • Ah I guess the issue. The names are different of both file and Class itself. – Volatil3 Jan 16 '14 at 17:31
  • I know this is 2 years late, but this: "it's the greatest thing about Laravel" isn't exactly true when you're building an app to work with a pre-existing unchangeable database that uses composite keys. Then Eloquent becomes the worst thing about Laravel since it has no way to handle composite keys, and no plans to do so. – LittleTreeX Jul 28 '16 at 19:29
  • I dont do much php/laravel anymore but you're right; no composite keys support. I cant remember exactly what we did to solve but there are ways of hacking queries to work with composite keys but not optimally. We also found issues querying multiple databases and using trashed values on relations. The key point is elequont does need to be married to your data and will not work for every use case. For the 99% of users who do have control over there data it will speed up development for 99% of use cases. for the other 1% you have the query builder and trusty raw SQL to fall back on :) – SwiftD Aug 17 '16 at 15:30
2

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

m1kfb
  • 96
  • 8
  • Guess it's not possible http://stackoverflow.com/questions/16753029/laravel-4-is-it-possible-to-extend-the-db-class – Volatil3 Jan 14 '14 at 11:15
  • 1
    You should not extend the DB class instead. Then it is possible. Just use the DB class inside your Model methods. – Ronald Hulshof Jan 14 '14 at 14:00