SQLSTATE [42S22]的唯一抛出:未找到列:1054未知列

时间:2017-12-20 08:15:15

标签: laravel

迁移后发生错误:刷新

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from `phone` where `phone_number` = 0883991010 and `id` <> )

我不知道,但也许是因为独特的验证

这是我对控制器的要求

   if ($this->method() == 'PATCH') {
        $name_rules = 'required|string|size:4|unique:student,name,' . $this->get('id');
        $phone_rules = 'sometimes|numeric|digits_between:10,15|unique:phone,phone_number,' . $this->get('id') . ',id_student';
    }

    else {
        $name_rules = 'required|string|size:4|unique:student,name,';
        $phone_rules = 'sometimes|numeric|digits_between:10,15|unique:phone,phone_number,';
    }

Phone.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    //
    protected $table = 'phone';

    protected $primaryKey = 'id_student';

    protected $fillable = [

        'id_student',
        'phone_number',
    ];



    public function student()
    {
        return $this->belongsTo('App\Student', 'id_student');
    }
} 

模式

public function up()
{

    Schema::create('phone', function (Blueprint $table) 
    {

        $table->integer('id_student')->unsigned()->primary('id_student');
        $table->string('phone_number')->unique();
        $table->timestamps();

        $table->foreign('id_student')->references('id')->on('tudent')->onDelete('cascade')->onUpdate('cascade');
    }); 

}

当我摆脱独一无二的时候它工作正常:phone,phone_number,

有什么更好的建议吗?

1 个答案:

答案 0 :(得分:0)

您需要在每个验证检查中指定自定义主键,但只在其中进行。

试试这个

   if ($this->method() == 'PATCH') {
        $name_rules = 'required|string|size:4|unique:student,name,' . $this->get('id'). ',id_student';
        $phone_rules = 'sometimes|numeric|digits_between:10,15|unique:phone,phone_number,' . $this->get('id') . ',id_student';
    }

    else {
        $name_rules = 'required|string|size:4|unique:student,name,' . $this->get('id'). ',id_student';
        $phone_rules = 'sometimes|numeric|digits_between:10,15|unique:phone,phone_number,' . $this->get('id') . ',id_student';
    }