DB :: table('table')和model::('table')之间的区别 [英] Difference between DB::table('table') and model::('table')

查看:549
本文介绍了DB :: table('table')和model::('table')之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel上,我们可以使用DB::table('table')->get();model::('table')->all();进行访问 我的问题是它们之间有什么区别?

on laravel we can access by using DB::table('table')->get(); or using model::('table')->all(); My question is what's the difference between them ?

谢谢.

推荐答案

之所以可以这样做,是因为ModelDB外观都实现了产生Builder实例的功能.

You can do this because Model and the DB facade both implement functions that yield a Builder instance.

https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Model.html
https://laravel.com/api/5.2/Illuminate/Database/Query /Builder.html

区别在于,Model的实例具有的属性可以设置带有预定信息的Builder,类似 的表,并为其提供事件,关系信息,特定的静态绑定以及一堆其他方便的助手,这些助手可以约束对象并简化面向对象的编程.

The difference is, instances of Model have properties which set up a Builder with predesignated information, like table, and also provide it with events, relationship information, specific static bindings, and a bunch of other handy helpers that constrain to objects and make object-oriented programming easier.

所以是的,您可以使用模型,然后获取查询Builder对象并更改其表(就像您可以更改有关Builder的其他任何内容一样),但是它正在与专门设计用于进行查询构建的系统抗衡更容易.

So yes, you can use a model and then take the query Builder object and change its table (just like you can change anything else about a Builder), but it's fighting a system specifically designed to make query building easier.

从本质上讲,Laravel所做的是采用Symfony2框架并对其进行简化,从而使一切变得更加简单.模型就是这种情况的一个例子.

At heart, what Laravel does is take the Symfony2 framework and streamline it so everything is simpler. Models are one such instance of this.

这篇关于DB :: table('table')和model::('table')之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆