jeudi 26 juillet 2018

Laravel Eloquent - query joint tables contains a same col name

I considered an issue when viewing data called from database using two related tables users and roles which have this structure: enter image description here

  • the users.role column contains an integer referring to the role record of roles table
  • the roles.name column contains the role name i.e. (user / admin / editor)

in my controller I used the laravel docs to build my query as I need to show a table in blade that holds users.name , users.email and roles.name that related to user by the schema:

$users = DB::table('users')
        ->join('roles', 'users.role', '=', 'roles.id')
        ->select('users.name', 'users.email', 'roles.name')
        ->get();

actually it works and dumping data, but the issue is the confusion of the two name alike cols users.name and roles.name. it dumping only the roles.name value like this sample record:

{"name":"user","email":"user@asd.com"},{"name":"user","email":"new@asd.com"},{"name":"user","email":"jaeden93@example.org"}

even when I tried to select all cols of users table like below:

$users = DB::table('users')
        ->join('roles', 'users.role', '=', 'roles.id')
        ->select('users.*', 'roles.name')
        ->get();

the users.name col still not showing!

Is there a way to solve this issue without changing table's column titles ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Af0kq2
via IFTTT

Aucun commentaire:

Enregistrer un commentaire