How to access db views using Laravel models? How to access db views using Laravel models? laravel laravel

How to access db views using Laravel models?


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.