Здравствуйте. Я создаю laravel бэкэнд-API и хочу отобразить пользовательскую модель в бизнес-модель, используя laravel отношение один к одному, сопоставление успешно, но я не хочу повторяющихся записей (внешний ключ таблицы пользователя) на бизнес-столах
Я пытаюсь сделать так, чтобы метод валидатора был уникальным, но, похоже, он не работает. Я получаю сообщение об ошибке
SQLSTATE [23000]: нарушение ограничения целостности: 1062 Повторяющаяся запись '1 'для ключа' business_user_id_unique '(SQL: вставить в businesses
(user_id
, business_name
, business_type
, business_about
, business_serial
, business_image
, business_approved
, account_balance
, account_total
, bank_name
, account_number
, updated_at
, created_at
) значения (1, школы Vickkys, школа, osksns dsdsnksds, saiaaja, h8einsnaksnai, 0, 0, 0, aisasna, 324354655343, 2020-04-02 12:48:55, 2020-04-02 12:48:55))
модель пользователя
<?php
namespace App;
use App\Notifications\PasswordResetNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable, HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname', 'lastname', 'phone', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function sendPasswordResetNotification($token)
{
$this->notify(new PasswordResetNotification($token));
}
public function business()
{
return $this->hasOne('App\Business');
}
}
модель бизнеса
приложение пространства имен;
use Illuminate \ Database \ Eloquent \ Model; использовать приложение \ пользователь; класс Business расширяет модель {
protected $fillable = ['business_name', 'user_id', 'business_type', 'business_about', 'business_serial',
'business_image', 'business_approved', 'account_balance', 'account_total', 'bank_name', 'account_number'];
public function user()
{
return $this->belongsTo('App\User');
}
businesscontroller@store
public function store(Request $request)
{
$id = Auth::id();
$request->user_id = $id;
$request->validate([
'user_id' => 'unique:businesses',
'business_name' => 'required|unique:businesses|max:255|string',
'business_type' => 'required|string|max:255',
'business_about' => 'required|string',
'business_serial' => 'required|string|max:15',
'business_image' => 'required|string',
'business_approved' => 'required|boolean',
'bank_name' => 'required|string|max:255',
'account_number' => 'required|integer',
]);
$user = User::find($id);
$business = New business;
$business->user_id = $request->user_id;
$business->business_name = $request->business_name;
$business->business_type = $request->business_type;
$business->business_about = $request->business_about;
$business->business_serial = $request->business_serial;
$business->business_image = $request->business_image;
$business->business_approved = false;
$business->account_balance = 0;
$business->account_total = 0;
$business->bank_name = $request->bank_name;
$business->account_number = $request->account_number;
$newbiz = $user->business()->save($business);
return response(['business' => $newbiz, 'user' => $user]);
}
Пожалуйста, помогите мне пользователь может зарегистрировать только один buisenes