我从一个相当大的数据库中提取,出于安全考虑,我的数据库用户只能从学生表中选择有限数量的列:name,gradu_date和gender。 但是在select *语句中返回了许多其他列。
在常规SQL中,如果我运行类似:
SELECT * FROM students将在该表上返回错误。 如果我运行雄辩的模型,也是如此
Students::all();也会返回错误。
我知道在Eloquent中,您可以在定义类似于以下的关系时限制您的选择:
class Students extends Eloquent { protected $table = 'student_info'; public function classes() { return $this->hasMany('classes')->select(array('room', 'time')); } }所以,我的问题是,可以在主模型上完成选择限制,类似于在类表上限制它。 所以,当我运行Student::all(); 它只选择我需要的列。
主要问题是每次我运行学生查询时,我每次都要做一个特定的选择命令,而不是只说“ Student::all() ”。 Student :: find(1)也是如此; 也会返回错误,因为它仍然运行SELECT * FROM student_info WHERE id = 1 。
我尝试设置$visible变量,但它仍然使sql等效于SELECT * FROM ...
有人有解决方案吗?
更新:请注意我在模型级别上寻找或解决方案,而不是控制器级别。 我可以从控制器端进行选择,但是这违背了Model概念的目的,并且必须声明要在每个查询中选择的列。
谢谢! 特洛伊
I'm pulling from a rather large database and for security reasons, my database user can only select a limited number of columns from the student table: name, graduation_date, and gender. But there are dozens of other columns returned in a select * statement.
In regular SQL, if I run something like:
SELECT * FROM studentswill return an error on that table. Same if I run the eloquent model
Students::all();will return an error as well.
I know in Eloquent, you can limit your selects when defining a relationship similar to:
class Students extends Eloquent { protected $table = 'student_info'; public function classes() { return $this->hasMany('classes')->select(array('room', 'time')); } }So, My question is, can the select limits be done on the main model, similar to limiting it on the classes table. So, when I run Student::all(); it only selects the columns I need.
The main problem is every time I run a student Query, I'm having to do a specific select command each time instead of just saying "Student::all()". Same thing for Student::find(1); will also return an error, because it still runs a SELECT * FROM student_info WHERE id = 1.
I tried setting $visible variable, but it still renders sql equivalent to SELECT * FROM ...
Anyone have a solution?
UPDATE: Please note that I'm looking or a solution on the model level, not the controller level. I can select from the controller side, but that defeats the purpose of a Model concept and have to declare the columns to select at every query.
Thanks! Troy
最满意答案
您可以创建一个中间类并重载all()函数。 我们称这个班为Elegant
Elegant.php
abstract class Elegant extends Model { public static $returnable = []; public function all() { return $this->get(static::$returnable)->all(); } }然后扩展此类,并为其定义可返回列。
Student.php
<?php class Student extends Elegant { public static $returnable = ['room', 'time']; }现在可以根据需要使用它:控制器中的Student::all() 。 如果你将returnable空数组,那么你将获得一切。
You can create an intermediate class and overload the all() function. Let's call this class Elegant
Elegant.php
abstract class Elegant extends Model { public static $returnable = []; public function all() { return $this->get(static::$returnable)->all(); } }Then you extend this class, and define your returnable columns to it.
Student.php
<?php class Student extends Elegant { public static $returnable = ['room', 'time']; }Now use it as you wanted: Student::all() in your controller. If you leave returnable as an empty array, then you will get everything.
更多推荐
发布评论