是否可以使用 laravel 特征从调用它们的模型中检索数据
基本上我正在做一个“企业应用程序”,我想让代码尽可能灵活,同时保持一定的可扩展性(目前供个人使用,所以我不需要非常专业)
问题是许多实体共享相同的字段因为我试图更深入地学习 Laravel,所以我认为这些模型(以及通过扩展的特征)的行为就像我之前工作过的一些 ORM,他们会为我创建 sql 东西......他们不所以我开始进行迁移,但现在我看到,如果我可以访问特征中的某些列,它将使代码更加清晰和更容易理解,
基本上我想做的是
trait hasValue{
public function getStuff(){
$ret = ModelAttachedTo::select("value", "price")->get();
return $ret;
}
}
编辑:我最终意识到,由于 Laravel 的这个和其他缺点,完全改回来结束,尽管如果有答案,我会尝试看看它是否符合问题并按预期工作
Basically im doing a "enterprise application" and i want to make code as flexible as possible while keeping it somewhat scalable(its for personal use for now so i don't need to be ultra professional)
The thing is many entities share the same fields and since im trying to learn laravel more in depth i thought that the models (and by extention the traits) acted like some ORM's i've worked before where they would create the sql stuff for me.... they don't so i started doing the migrations but now i see that if i could acess certain columns in the traits it would make the code a lot cleaner and more understandable
basically what i want to do is something like
trait hasValue{
public function getStuff(){
$ret = ModelAttachedTo::select("value", "price")->get();
return $ret;
}
}
Edit: I ended up realizing that with this and other shortcomings of laravel to just completely change back end although if a answer comes i will try to see if it fits the question and works as intended
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道在 Laravel 中是否有更简单的方法来做到这一点,但在 PHP 世界中,你可以使用抽象方法来做到这一点。 Traits 可以声明必须由类实现的抽象方法。
因此在trait中声明一个抽象的“getPrice”方法。您的所有类都必须实现该方法。您可以随时调用它。
I don't know if there's simplier way to do this in laravel but in PHP world, you can do this with abstract methods. Traits can declare abstract methods which have to implemented by classes.
So declare an abstract "getPrice" method in trait. All of your classes have to implement that method. And you can call it whenever you want.