Using AS Keyword in Laravel Eloquent Join Query In Main Model

141
December 16, 2018, at 1:10 PM

I want to use AS keyword in Laravel Eloquent Model like that:

Here my sample query:

User::find(1)
    ->select("name", "age")
    ->join("profile as p", "p.user_id", "=", "users.id")
    ->orderBy("name", "asc")
    ->get();

Expected usage u.id instead of users.id on joins with main table/model

->join("profile as p", "p.user_id", "=", "u.id")

Is there such a feature, is it possible?

Answer 1

use from method in eloquent like that

User::from('users as u')->find(1)
    ->select("name", "age")
    ->join("profile as p", "p.user_id", "=", "u.id")
    ->orderBy("name", "asc")
    ->get();

see this query eloquent method here

Rent Charter Buses Company
READ ALSO
do custom functionality after login in magento 2

do custom functionality after login in magento 2

I was trying to execute a custom functionality, after successful login of customer in magento 22 site

134
laravel request index can't list array Categorized

laravel request index can't list array Categorized

Note: my question is not duplicate

85
Query Working Fine MS SQL but Giving Error in PHP

Query Working Fine MS SQL but Giving Error in PHP

i am executing above QUERY in php it is giving error

142
PHP MySQL “id” Conflict and Uncaught PDOException

PHP MySQL “id” Conflict and Uncaught PDOException

Firstly, I'm experiencing ID conflictIn the database, I have a table called "admin" with row called "id"

130