简体   繁体   English

在模型上设置表格以获取最新记录-Laravel

[英]Set table on model for get last record - laravel

I need to set dynamically the table name (extract from other database) and get the last record. 我需要动态设置表名(从其他数据库中提取)并获取最后一条记录。

This is my model: 这是我的模型:

class Project extends Model {

    protected $connection = 'myconn';
    protected $table = '';

}

I tried that way (in other class): 我尝试过这种方式(在其他班级):

$last = Project::table($tableName)->orderBy('upload_time', 'desc')->first();

But this is the error: 但这是错误:

Call to undefined method Illuminate\Database\Query\Builder::table()

Thanks for helping 感谢您的帮助

The Eloquent query is like: Eloquent query就像:

$flights = App\Flight::where('active', 1)
               ->orderBy('name', 'desc')
               ->take(10)
               ->get();

but you are using it like Query Builder way, ie 但是您使用它的方式类似于Query Builder ,即

DB::table('tablename')->orderBy('id', desc)->first();

which is wrong. 这是错误的。

you can use query builder 您可以使用查询生成器

DB::table($tableName)->orderBy('upload_time', 'desc')->first(); DB :: table($ tableName)-> orderBy('upload_time','desc')-> first();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM