这个怎么样?没有测试过,但我认为应该可行.
class CreateMyView extends Migration { public function up() { DB::statement( 'CREATE VIEW myview AS SELECT [your select statement here]' ); } public function down() { DB::statement( 'DROP VIEW myview' ); } }
然后你可以创建一个模型来访问它:
class MyView extends Eloquent { protected $table = 'myview'; }
然后,要从应用中的其他位置访问该视图,您可以像查询任何其他模型一样查询该视图,例如
MyView::all(); // returns all rows from your view MyView::where( 'price','>','100.00' )->get(); // gets rows from your view matching criteria
道具转到下面提供了有关如何执行此操作的信息:
http://laravel.io/forum/05-29-2014-model-with-calculated-sql-field-doesnt-paginate
http://forumsarchive.laravel.io/viewtopic.php?pid=51692#p51692
警告
如果以后的迁移会修改视图下的表,请务必小心.原因是per the documentation:
The view definition is “frozen” at creation time,so changes to the underlying tables afterward do not affect the view definition. For example,if a view is defined as SELECT * on a table,new columns added to the table later do not become part of the view.
真的,我想你必须小心这样的东西才能进行任何迁移,所以也许这不是什么大问题.