我想从用户表laravel中选择我的余额“列”

问题描述 投票:0回答:1

我是laravel框架的初学者,我在开发API的过程中,当我在api中获取她的Id时,我想从用户表中选择用户的平衡所以我做了我在我的控制器和我的文档中找到的内容使用邮递员测试我的工作,但总是我得到一个错误这是我的控制器:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class MyBalanceController extends Controller
{
    public function index(Request $request)
    {
        # code...
        //  $Ads = ads::all();
        //  return $this->sendResponse($Ads->toArray(), 'Ads read succesfully');
        // This is the name of the column you wish to search
        $input = $request->all();
        $validator =    Validator::make($input, [
            'user_id'=> 'required'
        ] );

        $Cards = User::where('user_id','=', $request->user_id)->pluck('balance')->toArray();
        //$user = Auth::user();
       // $Cards = DB::select('select balance from users where id = :id', ['id' => 1]);


        return response()->json(['Cards'=>$Cards]);
    }
}

这是我的模态:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','username','lastname','tel','adress','balance'
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}
php laravel laravel-5 laravel-5.6
1个回答
0
投票

首先,您可以转到.env文件并将APP_DEBUG设置为true,这样您就可以在开发应用时看到异常。

关于你的问题试试

$balance = User::findOrFail($request->user_id)->balance;
return response()->json($balance);

如果未找到具有该id的用户,则将抛出404 HTTP错误

© www.soinside.com 2019 - 2024. All rights reserved.