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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题