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

问题描述 投票:0回答:1

迁移后发生错误:刷新

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_number,

还有更好的建议吗?

laravel
1个回答
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';
    }
© www.soinside.com 2019 - 2024. All rights reserved.