1

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Eloquent determines the foreign key of the relationship based on the model name. In this case, the Phone model is automatically assumed to have a user_id foreign key. If you wish to override this convention, you may pass a second argument to the hasOne method so there is phone class and as documentaion say it can determine foreign key, so why it is not in case of sentinal. Take a look there laravel.com/docs/5.3/eloquent-relationships
i mean it is not a bug there is an alternative avilable to pass the key but i am just curious to know that
I'm not exactly sure, not having used Sentinel at all. It may be a class quirk.
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.
yes i think that's the reason as the sufix name was also Eloquent_

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.