【问题标题】:Laravel 5.3 ErrorException in Model.php line 2709Model.php 第 2709 行中的 Laravel 5.3 ErrorException
【发布时间】:2018-06-10 05:57:12
【问题描述】:

我正在使用 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);
}

模型/用户.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');
  }
}

App\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 行中的错误异常: 关系方法必须返回 Illuminate\Database\Eloquent\Relations\Relation 类型的对象(查看:/var/www/html/group_map_v1/resources/views/emails/confirmation.blade.php)

【问题讨论】:

  • token 是该模型上的方法吗?
  • 我在下面回答了你的问题。如果您仍然无法解决此问题,请显示User 型号。
  • @lagbox 没有令牌不是方法它只是用户表中的一个字段
  • 错误似乎不这样认为......提供您的用户模型,因为这是相关部分
  • 一个 User 模型默认没有 createToken 方法,所以这不是 Laravel 自带的默认 User 模型

标签: laravel laravel-5 laravel-5.3


【解决方案1】:

您收到此错误,您尝试使用模型方法作为关系,但此方法不返回一个。关系应如下所示:

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

更新

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

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

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

所以,您要做的是将users 表中的token 属性重命名为其他名称。

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

【讨论】:

    【解决方案2】:

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

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

    asklagbox - blog - eloquent misunderstandings - dynamic properties and relationships

    【讨论】:

    • 你说得对,我只是把token字段的名字改成了verification_token就可以了!
    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 2018-04-24
    • 2015-07-05
    • 2017-02-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多