从 laravel 查询 Eloquent 获取未知列

Getting unknown column from laravel query Eloquent

我正在尝试建立用户和 otp 之间的一对多关系,但出现未知列错误。 我正在使用 Sentinel 进行用户身份验证。

OTP型号

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Sentinel;

class Otp extends Model
{

    protected $fillable = ['user_id','phonenumber','otp'];

    public function user()
    {
      return $this->belongsTo(Sentinel);
    }
}

哨兵用户模型。

namespace Cartalyst\Sentinel\Users;

........
.......

    protected $table = 'users';

    /**
     * {@inheritDoc}
     */
    protected $fillable = [
        'username',
        'password',
        'permissions',
        'relationship',
        'status',
        'phone'
    ];


    public function otp()
    {
        return $this->hasMany('App\Otp');
    }

.....

otp 架构

  Schema::create('otps', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('phonenumber');
            $table->string('otp');
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')
            ->onDelete('cascade')
            ->onUpdate('cascade');
        });

哨兵用户架构。

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');

        $table->string('username');
        $table->string('password');

        $table->string('name')->nullable();
        $table->bigInteger('phone')->nullable();
        $table->integer('birth_month')->nullable();
        $table->integer('birth_year')->nullable();
        $table->integer('birth_day')->nullable();
        $table->integer('relationship')->unsigned();
        $table->integer('status')->nullable();

        $table->text('permissions')->nullable();
        $table->timestamp('last_login')->nullable();

        $table->timestamps();

        $table->engine = 'InnoDB';
        $table->unique('username');
    });

所以我正在接受来自用户的请求,其中包含他的 phone 号码,我正在尝试将其存储在 otp table

public function phoneupdate(Request $request){

  $this->validate($request, [
      'phone' => 'bail|required|numeric|digits:10',
  ]);

  $user = Sentinel::findById(3);

  $randomOtp = rand (999 ,10000);

  $user->otp()->create([
    'phonenumber' => $request->phone,
    'otp' => $randomOtp,
  ]);

  return 'OK';
}

但给出 错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'eloquent_user_id' in 'field list' 
(SQL: insert into `otps` (`phonenumber`, `otp`, `eloquent_user_id`, `updated_at`, `created_at`) values (1234567890, 5997, 3, 2016-12-23 11:34:55, 2016-12-23 11:34:55))

eloquent_user_id 是它需要 user_id 而不是 eloquent_user_id 的问题,但根据 laravel 文档,它默认采用外键 user_id那么为什么会出现此错误

如果我更改

,代码工作正常

来自

public function otp()
{
  return $this->hasMany('App\Otp');
}

public function otp()
{
  return $this->hasMany('App\Otp','user_id');
}

那么为什么我需要定义 user_id 如果它默认取自 user_id.

由于您传递的是 Sentinel 而不是用户 class,您可能需要发送其加入的列。

public function user()
{
  return $this->belongsTo(Sentinel, 'id', 'user_id');
}

如果这不起作用,请尝试将列添加到 Sentinel\User class。

public function otp()
{
    return $this->hasMany('App\Otp', 'user_id');
}

深入研究源代码,UserInterface 使用模型 Cartalyst\Sentinel\Users\EloquentUser,因此它使用该模型名称来确定外键。