如何在Laravel中返回数据库表名 [英] How to return database table name in Laravel

查看:2044
本文介绍了如何在Laravel中返回数据库表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法,我可以得到我正在使用的模型使用的当前数据库表?我看到Laravel / Database / Eloquent / model.php中有一个table()函数,但是我一直没有成功调用它从我所在的模型中调用它。

解决方案

是 - 口才有一个 $ table 变量。有两种方式可以访问这个:

  class yourModel extends Eloquent {

public static $ table =differentTable;

函数someFunction()
{
dd(yourModel :: $ table);
}
}





  class yourModel extends Eloquent {

public function someFunction()
{
dd($ this-> table());

}
}

然后在你的代码

  Route :: get('/',function(){
$ model = new yourModel();
dd($ model-> someFunction());
});


Is there a way that I can get the current database table in use by the model that I'm in? I see that there is a table() function in Laravel/Database/Eloquent/model.php but I've been unsuccessful calling it calling it from the model that I'm in.

解决方案

Yes - Eloquent has a $table variable. There are two ways you can access this:

class yourModel extends Eloquent {

        public static $table = "differentTable";

        function someFunction()
        {
             dd(yourModel::$table);
        }
}

or

class yourModel extends Eloquent {

    public function someFunction()
    {
        dd($this->table());

    }
}

then in your code

Route::get('/', function () {
    $model = new yourModel();   
    dd($model->someFunction());
});

这篇关于如何在Laravel中返回数据库表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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