Laravel alias for table name using Eloquent

Posted by LaravelIndia - 4 years ago

Example 1:

$products = DB::table('really_long_table_name')
->select('really_long_table_name.id')
->get();


$products = DB::table('really_long_table_name AS tname')
->select('tname.id AS pid')
->get();

Example 2:

How to alias the name of a column in Eloquent

Products::where("actice", "=", true)
    ->joinWithTags
    ->get(['tags.name AS tag_name', 'products.*'])
    ->toArray();