How to alias a table in Laravel Eloquent queries (or using Query Builder)?

后端 未结 6 1837
谎友^
谎友^ 2020-11-28 02:58

Lets say we are using Laravel\'s query builder:

$users = DB::table(\'really_long_table_name\')
           ->select(\'really_long_table_name.id\')
                 


        
6条回答
  •  误落风尘
    2020-11-28 03:19

    To use in Eloquent. Add on top of your model

    protected $table = 'table_name as alias'

    //table_name should be exact as in your database

    ..then use in your query like

    ModelName::query()->select(alias.id, alias.name)

提交回复
热议问题