How to access db views using Laravel models?

后端 未结 1 417
不思量自难忘°
不思量自难忘° 2021-02-07 18:14

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 pr

相关标签:
1条回答
  • 2021-02-07 18:24

    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;
    

    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.

    0 讨论(0)
提交回复
热议问题