Laravel-调用未定义的方法App \\ User :: getAvatarUrlAttribute()

时间:2019-09-17 13:45:32

标签: laravel api postman

我正在使用Laravel-5.8作为应用程序的后端。我已经为端点编写了所有的Api。

Laravel:ApiController

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\User;
use App\Activity;
use Avatar;
use Storage;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Mail;
use Audit;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;


class ApiController extends Controller
{
public $successStatus = 200;

public function __construct() {

}  

protected function guard()
{
    return Auth::guard();
}  

protected function respondWithToken($token)
{
    return response()->json([
        'access_token' => $token,
        'token_type' => 'bearer',
        'expires_in' => auth()->factory()->getTTL() * 60,
        'user' => auth()->user()->email
    ], 200);
} 

public function returnResponse($success, $data, $errorCode = 0, $message = false) {
    $response = array();
    $response['success'] = $success;
    $response['message'] = isset($message) ? $message : '';
    if ($errorCode) {
        $response['errorCode'] = isset($errorCode) ? $errorCode : 0;
    }
    $response['data'] = $data;
    return response()->json($response, 200);
}

public function register(Request $request) {
    $validator = Validator::make($request->all(), [
                'name' => 'required',
                'email' => 'required|string|email|max:255|unique:users',
                // 'phone' => 'required',
                // 'password' => 'required',
                'password' => 'required|string|min:6',
                // 'password' => 'required|string|min:6|confirmed',
                'password_confirmation' => 'required|same:password',
    ]);
    if ($validator->fails()) {
        return $this->returnResponse(false, ['error' => $validator->errors()], 1, 'Invalid User Data');
    }
    $input = $request->all();


    // code for check email / username / phone exist or not
    if(isset($input['email'])){

        $alreadyExist = User::where(function ($query) use ($input) {
                $query->where('email', '=', $input['email']);                           
            })->get();
    }

    if (count($alreadyExist->toArray()) > 0) {
        return $this->returnResponse(false, ['error' => 'Email Already Exist'], 1, 'User Data Already Exist');
    }


    // code for register user
    $user = new User();
    $user->name = $input['name'];
    $user->email = $input['email'];
    $user->password = bcrypt($input['password']);
    $user->save();

        $mainData = array();
        $mainData['to'] = $user->toArray()[0]['email'];
        $mainData['from'] = "support@tsllimited.com";
        $mainData['subject'] = "Successful Signup";
        $mainData['content'] = "Your signup was successful, you can login with the credentials.";

        $this->mailSend($mainData);

        Activity::create([
            'user_id'   =>  $user->id,
            'owner_id'  =>  $user->client_id,
            'type'      =>  "User Registration",
            'title'     =>  "Successful Signup of User",
            'state'     =>  2,
            'created_at'=>  date('Y-m-d H:i:s')
        ]);


    $success = array();
    $success['user_id'] = $user->id;
    $success['user']=$user;

    return $this->returnResponse(true, $success, 0, 'User registered successfully');
}


public function login(Request $request) {
    $authenticated = false;

    $validator = Validator::make($request->all(), [
        'email' => 'required|string|email',
        'password' => 'required|string',
        'remember' => 'boolean'
    ]);
    if ($validator->fails()) {
        return $this->returnResponse(false, ['error' => $validator->errors()], 1, 'Invalid User Data');
    }


    $remember = request('remember') ? true : false;
    if (Auth::guard('web')->attempt(['email' => request('email'), 'password' => request('password')], $remember)) {
        $authenticated = true;
    } 
    if ($authenticated == true) {
        $user = Auth::guard('web')->user();
        $date = date('Y-m-d');

        $success['userId'] = $user->id;
        $success['avatar'] = url('/storage/user') . '/' . $user->avatar;
        $success['email'] = $user->email;
        $success['token'] = $user->createToken('MyApp')->accessToken;
        return $this->returnResponse(true, $success);
    } else {
        $success = array();
        return $this->returnResponse(false, $success, 1, 'Invalid User Credential');
    }
} 
}

api.php

Route::group([
], function () {
Route::post('login', 'ApiController@login');
Route::post('register', 'ApiController@register');
Route::post('forgetPassword', 'ApiController@forgetPassword');

Route::group([
 'middleware' => 'auth:api'
], function() {
Route::get('logout', 'AuthController@logout');
Route::get('user', 'AuthController@user');
});
});

我停止并配置了Laravel Passport和Spatie。我已经检查了代码,不知道真正的错误是什么。当我在POSTMAN上测试重新发布的Post Request时,出现以下错误:

postman error

请参阅POSTMAN预览端:

postman error2

是什么原因导致该错误,我该如何解决?

3 个答案:

答案 0 :(得分:0)

您的avatar表上没有列users

答案 1 :(得分:0)

也许您没有在$course->courseinstances->start_end_date['startdate'] 类中使用所需的trait

User

答案 2 :(得分:0)

我最终自己解决了这个问题。问题是,我忘了补充:

public function getAvatarUrlAttribute()
{
    return Storage::url('avatars/'.$this->id.'/'.$this->avatar);
}

到用户模型。

谢谢