我正在尝试更新" contrato"来自用户,但我收到此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ' contrato' in 'where clause' (SQL: select count(*) as aggregate from `contratos` where ` contrato` = 3261026)
我试图解决但我无法看到我的错误。
在控制器中,我正在进行验证,我可以修改" contrato"忽略用户,这样我就不会得到错误"这个contrato存在。"
控制器代码
public function update(Request $request, $id)
{
$user = Auth::user();
$contrato = Contrato::find($id);
$request->validate([
'contrato' => [
'required','numeric','unique:contratos, contrato, '.$user->id' ','digits:7',
new ValidateContrato,
],
]
);
$contrato->update([
'contrato' => $request->contrato,
]);
session()->flash('message','¡Su contrato fue editado satisfactoriamente!');
return redirect()->route('contratos.index');
}
用户模型
class User extends Authenticatable
{
use Notifiable;
public function contrato(){
return $this->hasMany(Contrato::class);
}
public function card(){
return $this->hasMany(Card::class);
}
public function pago(){
return $this->hasMany(Pago::class);
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'lastname', 'alias', 'cedula', 'email', 'email2', 'password', 'cellphone', 'homephone', 'faxphone', 'officephone', 'born_date', 'terms_accepted', 'invoice_by_mail',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
contrato模型
class Contrato extends Model
{
public function user(){
return $this->belongsTo(User::class);
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id', 'contrato', 'user_id', 'emition_date', 'payment_reference',
];
}
答案 0 :(得分:1)
你的错误在于这一行:
'required','numeric','unique:contratos, contrato, '.$user->id' ','digits:7',
删除contrato
之前的空格。该行应如下所示:
'required','numeric','unique:contratos,contrato,'.$user->id' ','digits:7',
答案 1 :(得分:0)
请更新您的验证规则,如:
$request->validate([
'contrato' => [
'required,numeric,unique:contratos,contrato,'.$user->id.',digits:7',
new ValidateContrato,
],