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

后端 未结 6 1818
谎友^
谎友^ 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)

    0 讨论(0)
  • 2020-11-28 03:20

    Same as AMIB answer, for soft delete error "Unknown column 'table_alias.deleted_at'", just add ->withTrashed() then handle it yourself like ->whereRaw('items_alias.deleted_at IS NULL')

    0 讨论(0)
  • 2020-11-28 03:39

    Laravel supports aliases on tables and columns with AS. Try

    $users = DB::table('really_long_table_name AS t')
               ->select('t.id AS uid')
               ->get();
    

    Let's see it in action with an awesome tinker tool

    $ php artisan tinker
    [1] > Schema::create('really_long_table_name', function($table) {$table->increments('id');});
    // NULL
    [2] > DB::table('really_long_table_name')->insert(['id' => null]);
    // true
    [3] > DB::table('really_long_table_name AS t')->select('t.id AS uid')->get();
    // array(
    //   0 => object(stdClass)(
    //     'uid' => '1'
    //   )
    // )
    
    0 讨论(0)
  • 2020-11-28 03:41

    Here is how one can do it. I will give an example with joining so that it becomes super clear to someone.

    $products = DB::table('products AS pr')
            ->leftJoin('product_families AS pf', 'pf.id', '=', 'pr.product_family_id')
            ->select('pr.id as id', 'pf.name as product_family_name', 'pf.id as product_family_id')
            ->orderBy('pr.id', 'desc')
            ->get();
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-28 03:45

    To use aliases on eloquent models modify your code like this:

    Item
        ::from( 'items as items_alias' )
        ->join( 'attachments as att', DB::raw( 'att.item_id' ), '=', DB::raw( 'items_alias.id' ) )
        ->select( DB::raw( 'items_alias.*' ) )
        ->get();
    

    This will automatically add table prefix to table names and returns an instance of Items model. not a bare query result. Adding DB::raw prevents laravel from adding table prefixes to aliases.

    0 讨论(0)
  • 2020-11-28 03:45

    You can use less code, writing this:

        $users = DB::table('really_long_table_name')
           ->get(array('really_long_table_name.field_very_long_name as short_name'));
    

    And of course if you want to select more fields, just write a "," and add more:

     $users = DB::table('really_long_table_name')
           ->get(array('really_long_table_name.field_very_long_name as short_name', 'really_long_table_name.another_field as other', 'and_another'));
    

    This is very practical when you use a joins complex query

    0 讨论(0)
提交回复
热议问题