【问题标题】:Laravel 4 calling a function from a retrieved modelLaravel 4 从检索到的模型中调用函数
【发布时间】:2014-10-17 13:28:59
【问题描述】:

在 Laravel 4 中,我创建了两个数据库表:users 和 user_pending_updates

我创建了一个名为 User 的模型。如果我得到一个用户模型:

$user = User::where('email', '=', $email)->first();

我想在模型中调用一个函数:

    public static function isPendingUser()
    {
        $update = DB::table('user_pending_updates')->where('user_id', UNKNOWN)->where('pending_update', 1)->first();

        if(count($update) > 0)
        {
            return $update;
        }

        return false;
    }

我的问题是,如果我这样调用函数:

$user->isPendingUser();

如何在函数内捕获 $user->id 变量,以便将其添加到我指出 UNKNOWN 的位置?

谢谢你!

【问题讨论】:

  • 您应该检查null 而不是使用count。那会更快。单线($update = ...之后):return null === $update ? false : $update;

标签: php database laravel model eloquent


【解决方案1】:

删除静态修饰符,用户ID将是$this->id

【讨论】:

    猜你喜欢
    • 2013-07-04
    • 1970-01-01
    • 2021-01-18
    • 2014-01-20
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多