如何使用Laravel模型访问数据库视图? [英] How to access db views using Laravel models?

查看:553
本文介绍了如何使用Laravel模型访问数据库视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个支持Postgres的Web应用程序移植到Laravel 4.2,但是我看不到一种方法来利用psql db中内置的现有八个左右的求和视图.这些视图执行各种聚合和平均功能,因此它们说明了表实体之间的关系,因此是架构的适当组成部分.

I am porting a Postgres-backed web app to Laravel 4.2, and I can't see a way to utilize the existing eight or so summation views which are built in the psql db. These views preform various aggregation and averaging functions, and as such are properly part of the schema as they illustrate the relationships between the table entities.

确定有人必须通过Laravel的活动记录样式界面使用数据库视图吗?您如何处理?

Surely someone has had to use db views with the active record style interface of Laravel? How do you go about it?

推荐答案

您的问题与数据库视图有关,如果我没有记错,那么您正在谈论的是动态创建的动态表,例如,在MySql,则可以使用以下内容创建View:

Your question is about database views and if I'm not wrong then you are talking about the dynamic table that gets created on the fly, for example, in MySql, it's possible to create a View using something like this:

CREATE VIEW students AS SELECT * FROM profiles where type='student' ORDER BY id;

因此,它允许查询动态表,这里是students视图,例如:

So, it'll allow to query the dynamic table which is the students view here, for example:

select * from students;

这将从students视图返回过滤的数据.因此,如果我对您的问题是正确的,那么我认为您可以像使用真实表一样使用Eloquent,例如,为学生view创建一个Eloquent模型,您可以使用像这样的东西:

This will return the filtered data from students view. So, if I'm right about your question then I think you are able to use Eloquent just like you use for real tables, for example, to create an Eloquent model for students view you can simply create it using something like this:

class ViewStudent extends Eloquent {

    protected $table = 'students';
}

因此,现在您可以正常使用此模型,例如,可以将其用于其他表;

So, now you can use this model as usully you may use for other tables, for example;

$students = ViewStudent::all();

这是相同的方式.由于您要求输入psql,所以我不确定该语法或其在系统中的工作方式,但我相信这是可能的.

It's just the same way. Since you asked for psql so I'm not sure about the syntax of that or how it works in that system but I believe it's possible same way.

这篇关于如何使用Laravel模型访问数据库视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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