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.

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

推荐答案

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

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

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

select * from students;

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();

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相关问答推荐

如何在Laravel中创建自定义单项集合

如何返回Blade Laravel 中的按钮?

laravel Eloquent 模型更新事件未触发

Laravel 5.1 是否与 PHP 7 兼容

Laravel 查询构建器 - 如何按别名分组,或进行原始 groupBy

如何在使用 Laravel 在控制器中发送邮件之前更改邮件配置?

控制器中的 Laravel Auth 用户 ID

laravel 5 中的登录事件处理

Laravel 应用程序中应用程序键的意义是什么?

Laravel 项目旁边的 Wordpress 项目(在 public_html 文件夹中)

Laravel 无法创建根目录

Laravel Mix 生成字体到另一个目录

如何在 Laravel 或 Redis 中取消排队的作业(job)

Laravel Queue,Beanstalkd vs Database,有什么区别?

Laravel 4 验证唯一(数据库)忽略当前

控制器中的laravel foreach循环

Lumen/Laravel 6:调用未定义的函数 array_except()

Laravel 在域和子域中共享 cookie 检测问题

Laravel 集合计数结果

Laravel 4:分析器在哪里?