dsnm64969 2018-07-19 17:42
浏览 321
已采纳

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 2018-07-19 17:46
    关注

    You can do it with ->only:

    return response()->json([
        'data' => $user->only(['id', 'name', 'phone', 'email']),
        ...
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?