Laravel 5.3 Model.php第2709行{Error}错误

时间:2017-12-29 21:22:25

标签: laravel laravel-5 laravel-5.3

我正在使用Laravel 5.3,我想使用以下

发送验证邮件

php artisen make:auth
php artisen make:mail ConfirmationEmail

ConfirmationEmail.php

<?php

namespace App\Mail;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ConfirmationEmail extends Mailable
{
use Queueable, SerializesModels;

/**
 * Create a new message instance.
 *
 * @return void
 */
public $user;
public function __construct(User $user)
{
    $this->user = $user;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('emails.confirmation');
}
}

电子邮件/ confirmation.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sign Up Confirmation</title>
</head>
<body>
  <h1>Thanks for signing up!</h1>

  <p>
    We just need you to <a href='{{ url("register/confirm/{$user->token}") }}'>confirm your email address</a> real quick!
</p>
</body>
</html>

UserController.php

public function register(Request $request)
{
    $validator = Validator::make($request->all(), [
        'email'         => 'required|email|unique:users',
        'name'          => 'required|string',
        'password'      => 'required|string|min:6',
        'country'       => 'required'
    ]);
    if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);
    }
    $input = $request->all();
    $input['password'] = bcrypt($input['password']);
    $input['user_pin'] = $this->generatePIN();
    $user = User::create($input);
    Mail::to($user->email)->send(new ConfirmationEmail($user));
    $success['token'] =  $user->createToken('MyApp')->accessToken;
    $success['user']  =  $user ;
    $now      = Carbon::now();
    UserLocation::create(['user_pin' => $input['user_pin'] , 'lat'=> 0.0 , 'lng' => 0.0 , 'located_at' => $now]);
    return response()->json(['success'=>$success], $this->successStatus);
}

模型/ user.php的

<?php

vnamespace App\Models;
use App\User as BaseUser;

class User extends BaseUser
 {

  /**
  * The attributes that should be hidden for arrays.
  *
  * @var array
  */
  protected $hidden = [
    'password', 'remember_token',
  ];


  public function groups(){
    return $this->belongsToMany(Group::class, 'group_member', 'member_id', 'group_id');
  }

public function sentRequests(){
    return $this->hasMany(Request::class, 'from_user_pin', 'user_pin');
  }

public function receivedRequests(){
    return $this->hasMany(Request::class, 'to_user_pin', 'user_pin');
  }

public function locations(){
    return $this->hasMany(UserLocation::class, 'user_pin', 'user_pin');
  }
}

应用\ user.php的

<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;


class User extends Authenticatable
{
 use HasApiTokens, Notifiable;


protected $guarded = ['id'];

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
/*protected $fillable = [
    'name', 'email', 'password',
];*/

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
 protected $hidden = [
    'password', 'remember_token',
 ];

}

我现在收到此错误

  

Model.php第2709行中的ErrorException:   关系方法必须返回一个类型为Illuminate \ Database \ Eloquent \ Relations \ Relation的对象(查看:/var/www/html/group_map_v1/resources/views/emails/confirmation.blade.php)

2 个答案:

答案 0 :(得分:2)

token是模型上的一种方法。当您尝试访问模型上的动态属性时,它会查找属性,然后查找该名称的关系方法(或已加载的关系)。

您没有名为token的属性。当您尝试通过动态属性访问它时,它会查找名为token的方法(这是它可以通过该属性访问关系的方式)。当它执行此操作时,它会命中该方法,并且该方法不会返回关系类型对象。因此,Eloquent在那一点上断了,因为该属性是属性和关系,它不能对它做任何事情。

asklagbox - blog - eloquent misunderstandings - dynamic properties and relationships

答案 1 :(得分:1)

您收到此错误,您尝试将模型方法用作关系,但此方法不会返回错误。这种关系应该是这样的:

public function relationship()
{
    return $this->hasMany(Model::class);
}

<强>更新

HasApiTokens trait有一个名为token()的方法,它是一个简单的访问者:

public function token()
{
    return $this->accessToken;
}

执行$user->token时,Laravel会看到此方法并尝试将其用作关系。

因此,您要做的是将token表中的users属性重命名为其他内容。

感谢@lagbox指向正确的方向。