douzhenyu6533 2016-12-23 11:59
浏览 50
已采纳

从laravel查询中获取未知列Eloquent

I am trying a one to many relationship between user and otp but i got a unknown column error. and i am using Sentinel for user auth.

Otp model

<?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);
    }
}

Sentinel user model.

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

  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');
        });

sentinel user schema.

    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');
    });

so there i am accepting request from user which contains his phone number and i am trying to store it in 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';
}

but gives an error

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 is the problem it needs to be user_id instead of eloquent_user_id but as per laravel documentation it by default take the foreign key user_id so why it is giving this error

and code works fine if i change

from

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

to

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

so why i need to define user_id if it take by default from user_id.

  • 写回答

1条回答 默认 最新

  • duanlieshuang5330 2016-12-23 12:48
    关注

    Since you're passing Sentinel and not the user class, you may need to send the columns it's joined on.

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

    If that doesn't work, try adding the column to the Sentinel\User class.

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

    Diving into the source code, the UserInterface uses the model Cartalyst\Sentinel\Users\EloquentUser, so it's using that model name to determine the foreign key.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?