dsnm64969
dsnm64969
2018-07-19 17:42

Laravel如何使用auth返回所选列:user()

已采纳

From last few days, I have started learning Laravel with Passport and followed this wonderful article Create REST API in Laravel with authentication using Passport.

I have created the login, register, detail listing API but in the login API I am trying to return logged in user details in JSON and it returns all information but I want only id, name, email, phone so how to apply select('id', 'name', 'email', 'phone') on $user = Auth::user()

I can easily remove data using PHP unset function but I don't want to use that. I want to select only column which is required so is it possible?

My Controller Source Code:

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class PassportController extends Controller
{
    public function login()
    {
        if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
            $user = Auth::user();
            $success['token'] = $user->createToken('MyApp')->accessToken;

            return response()->json(['data' => $user, 'success' => $success], 200);
        } else {
            return response()->json(['error' => 'Unauthorised'], 401);
        }
    }
}

API JSON Result:

{
    "data": {
        "id": 1,
        "name": "Abraham Hess",
        "first_name": "Abraham",
        "last_name": "Hess",
        "phone": "982595360",
        "email": "abraham@mailinator.com",
        "username": "abraham",
        "created_at": "2018-05-29 16:58:55",
        "updated_at": "2018-05-29 16:58:55"
    },
    "success": {
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjFiYjQ3OWY3MzlkZTAxNjc0Njc4Nzg3Mjg2ZTU4NGI0NjljNjE2OGUxNGUyZWUzYmJlOWViMWIwMTUxMzhhZWI3ODU0NjViNTgzZTQ5ZjVlIn0.eyJhdWQiOiIxIiwianRpIjoiMWJiNDc5ZjczOWRlMDE2NzQ2Nzg3ODcyODZlNTg0YjQ2OWM2MTY4ZTE0ZTJlZTNiYmU5ZWIxYjAxNTEzOGFlYjc4NTQ2NWI1ODNlNDlmNWUiLCJpYXQiOjE1MzIwMjExMDYsIm5iZiI6MTUzMjAyMTEwNiwiZXhwIjoxNTYzNTU3MTA2LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.KpaU10j9mrvgLarYODEza9fd6f_7YI99GabnSIZj370M439abxqQWuQHH9LhUWy1OuFVaj5wt-BC0UIbirI3nkVaqgfwoldMT_xijv6ZuL0glLAvaNOfZwO-Oox8zZO1cDL35g49GT3MNqd8iFD6zLnaDrljsB_pkL2PVOqgRwdIVLamR_VUy_EvxhQhXPBsRrEVYQS1gGLj-voPV7iMZyTU4QrmnPZC1n_ChPZDt4nSUT__UDgXS7Bw0pV5RI03c91Lk_cOvgCbBZsLpYO1LtlXs-rKUoUbWr17xaAJ1MoPHeNVUHw931_ZOkwVX_gW_rimZWdQTuTOZwTVh4kV8rldR4IghDSY4kCVQccO_p6rAQrwy5qD-godkMxwn8mee8zsWpusPhfbJ_iWZH3LKDTHwKCBq6v4GUGipJTXgbiiBPg-MZ_bVjl2rPPdHRXMPpvMtxJChTeZ71tfVu7AbJ2GHvHU6gVm3RyEPDeGgmBt1WdyDi49Gw8weym8t5o-g33JAPLEOa73lg8h36NtFVmbXAR2P5oIV8pG1PXUNCtcGu_Lz8Zq1Kd-7AJBsFzMIELgRG2_CJCzEpir91cM9xfId-iqXQ7Vw1QWs8RDM5A7vESRh17UHcKHXyFk4srT0TGLkjTtsifUv6sA2AcHw3yJF6gc3nZoalTSghzJqA4"
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douxian6260 douxian6260 3年前

    You can do it with ->only:

    return response()->json([
        'data' => $user->only(['id', 'name', 'phone', 'email']),
        ...
    ]);
    
    点赞 评论 复制链接分享

为你推荐