如何访问保护Laravel类中的变量 [英] How to access protect variable within class Laravel

查看:280
本文介绍了如何访问保护Laravel类中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白如何存取同一个类别中的表格名称。

I don't understand how to access the table name within same class.

class Timesheets extends Model
{
    protected $table = "timesheets";

    public static function getAllTimesheets() {
        //return DB::table("timesheets")->get();
        return DB::table("timesheets")
            ->join('users', 'name', '=', 'users.name')
            ->select('timesheets.id', 'name', 'timesheets.time_start', 'timesheets.time_end', 'timesheets.time_desc')
            ->get();
    }
}

如何替换时间表表变量?

How can I replace the "timesheets" with the protected Table variable?

推荐答案

直接回答(new static) - > getTable $ c>



Direct Answer (new static)->getTable()

class Timesheets extends Model
{
    protected $table = "timesheets";

    public static function getAllTimesheets() {
        return DB::table((new static)->getTable())
            ->join('users', 'name', '=', 'users.name')
            ->select('timesheets.id', 'name', 'timesheets.time_start', 'timesheets.time_end', 'timesheets.time_desc')
            ->get();
    }
}






有机会了解详情



Eloquent模型使用 Magic Functions ,它允许你通过静态函数调用来检索一个新的类实例的非静态方法; Illuminate / Database / Eloquent / Model :: __ callStatic()


A Chance to Learn More

Eloquent Models use Magic Functions that will allow you to retrieve a new class instance's non-static methods via a static function call; Illuminate/Database/Eloquent/Model::__callStatic().


__ callStatic()在调用静态上下文中不可访问的方法时触发。

__callStatic() is triggered when invoking inaccessible methods in a static context.

查看 Illuminate / Database / Eloquent / Model :: __ callStatic()我们看到 $ instance = new static; ,它调用延迟静态绑定。这意味着您将获得的值将与新类实例化相同。由于 @ PeterPan666已评论,此操作只会如果所请求的方法根本不存在于当前类上,则为work。查看 Illuminate / Database / Eloquent / Model :: __ call()我们看到这个调用将被发送到一个新的查询

Looking at the code for Illuminate/Database/Eloquent/Model::__callStatic() we see $instance = new static; which invokes Late Static Bindings. This means that the value you will get will be the same as a new class instantiation. As @PeterPan666 commented, this will only work if the requested method does not exist on the current class at all. Looking at the code for Illuminate/Database/Eloquent/Model::__call() we see that this call will be sent to a new query Builder for the model's table.

ÁlvaroGuimarães回答,您可以使用 static :: join()开始查询模型的表格。

As Álvaro Guimarães answered, you can start a query for the model's table by using static::join().

class Timesheets extends Model
{
    protected $table = "timesheets";

    public static function getAllTimesheets() {
        return static::join('users', 'name', '=', 'users.name')
            ->select('timesheets.id', 'name', 'timesheets.time_start', 'timesheets.time_end', 'timesheets.time_desc')
            ->get();
    }
}

这篇关于如何访问保护Laravel类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆