Я хочу выбрать свой баланс "столбец" из таблицы пользователя laravel - PullRequest
0 голосов
/ 15 февраля 2019

Я новичок в фреймворке laravel, я нахожусь в процессе разработки API, и я хочу выбрать из пользовательской таблицы баланс пользователя, когда я получу ее идентификатор в API. Поэтому я делаю то, что нашел в документации в моемКонтроллер и я использую почтальон для проверки своей работы, но всегда я получаю сообщение об ошибке, это мой контроллер:

<?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 [];
    }
}

1 Ответ

0 голосов
/ 15 февраля 2019

Сначала вы можете зайти в свой файл .env и установить для APP_DEBUG значение true, чтобы вы могли видеть свои исключения при разработке приложения.

Что касается вашей проблемы, попробуйте

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

Если нетпользователь был найден с таким идентификатором, тогда будет выдана ошибка 404 HTTP

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...