[英]How to create Laravel Model without Eloquent?
As I am not sure, is it possible to create models with DB Class instead of Eloquent? 我不确定,是否可以使用DB Class而不是Eloquent创建模型? I want to stay away from ORM. 我想远离ORM。
Thanks 谢谢
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. 您不需要扩展任何类来创建一个封装业务逻辑的模型类,并且包含调用DB类的方法。
Just create your model inside app/models/MyModel.php
like this 只需在app/models/MyModel.php
创建你的模型就像这样
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. 如果你想扩展数据库类,你可能希望扩展Database / Builder类以扩展功能,但这是一个复杂的主题,我怀疑你会问一个非常不同的问题,如果这就是你之后。
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 正如我最后指出的那样,我不会避开Eloquent,这是Laravel在很多其他伟大事物中最伟大的事情
只需删除“extends Eloquent”并使用DB类构建查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.