php - 拉维尔 4 : How to use an Accessor in a where-clause

标签 php database laravel-4 eloquent accessor

是否可以在 Laravel 4 中使用访问器进行比较,例如:

class User extends Eloquent {

   // define Accessor
   public function getSpecialNameAttribute()
   {
       return 'Joda';
   }

}

$User = User::where('gender','=','male')->where('specialName','=','Joda');

?

最佳答案

是的,您可以使用它来过滤查询结果:

User
    ::where('gender','=','male')
    ->get()
    ->filter(function($item) {
        return $item->specialName === 'Luke';
    });

(!)请注意,过滤将在查询数据库后应用,因此在大数据的情况下,此解决方案将出现性能问题。

有关更多详细信息,我已经为您搜索了 this Collections tutorial .

另外我建议query scopes可能有助于以最佳方式完成您的任务。

关于php - 拉维尔 4 : How to use an Accessor in a where-clause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27255130/

相关文章:

php - 通过php表单向mysql数据库添加多个条目

PHP 从逗号分隔的字符串中以逗号获取值,而不会爆炸和循环

java - Android Studio-数据库

django - 与 pgAdmin 的远程 PostgreSQL 连接

database - Cloud Firestore 事务的限制

php - 简单的 PDO 包装器

php - 检查字符串的第一个字符是否为字母的函数

php - Laravel 4 的 Mthaml Composer 包 - 如何?

php - Laravel将可变成功消息从存储库传递回 Controller

php - 使用 Laravel 或 PHP 在 mysql 表上建立条件关系