首页 > 解决方案 > Laravel JWT return data from two tables

问题描述

I am using JWT authentication via api.php

when a user login to the route

Route::post('login', 'AuthController@login'); 

he reached this method in auth controller

protected function respondWithToken($token)
    {

        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth()->factory()->getTTL() * 1000,
            'user' => auth()->user()
        ]);
    }

Here 'user' => auth()->user() is returning the data from user table, I have another table user_Details in which all user information is saved, I want to fetch details from that table to when some one login,

Help me out please.

标签: databaselaravel-5jwt

解决方案


我找到了另一种简单的方法来做到这一点

protected function respondWithToken($token)
    {

        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth()->factory()->getTTL() * 1000,
            'user' => auth()->user(),
            'user_detail' => user_detail::where('user_id','=',auth()->user()->id)->get()
        ]);
        }

推荐阅读