laravel 如何在不输入字段employee_id的情况下存储外键?

zzzyeukh  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(119)

我有两个表,它们之间的关系是一对多
模范员工是:

class Employee extends Model implements HasMedia
{
    use HasFactory;

    protected  $guarded = [];

    protected $casts = [
        
    ];

public function familyDetails()
        {
            return $this->hasMany(FamilyDetails::class,'employee_id');
        }

模型FamilyDetails为:

class FamilyDetails extends Model
{
    use HasFactory;

    public function employee()
    {
        return $this->belongsTo(Employee::class,'employee_id');
    }

有一个接口要求我输入FamilyDetails表中的信息,但在该表中有一个employee_id字段将两个表连接在一起

这是控制器:

class FamilyDetailsController extends Controller
{
    public function store(StoreFamilyDetailsRequest $request)
    {
        $familyDetails = FamilyDetails::create($request->validated())->with('employee');
        return new FamilyDetailsResource($familyDetails);
    }
}

这是商店系列详细信息请求:

class StoreFamilyDetailsRequest extends FormRequest
{

    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'Name_kanji'                              => ['required'],
            'Relationship'                            => ['required'],
            'Nama_katakana'                           => ['required'],
            'Grade_In_school'                         => ['required'],
            'Date_of_birth*'                          => ['required|array'],
                'Date_of_birth.*day'                  => ['required'],
                'Date_of_birth.*year'                 => ['required'],
                'Date_of_birth.*month'                => ['required'],

        ];
    }

    public function validated($key = null, $default = null)
    {

        return [
               
                'Name_kanji'                        => $this->Name_kanji,
                'Nama_katakana'                     => $this->Nama_katakana,
                'Relationship'                      => $this->Relationship,
                'Grade_In_school'                   => $this->Grade_In_school,
                'Date_of_birth'                     => Carbon::create(
                    $this->Date_of_birth['year'],
                    $this->Date_of_birth['month'],
                    $this->Date_of_birth['day'])->format('Y-m-d'),

        ];
    }

这是家庭详细信息资源:

class FamilyDetailsResource extends JsonResource
{

    public function toArray($request)
    {

            return [
                
                'Name_kanji'                      => $this->Name_kanji,
                'Relationship'                    => $this->Relationship,
                'Nama_katakana'                   => $this->Nama_katakana,
                'Grade_In_school'                 => $this->Grade_In_school,
                'Date_of_birth'                        => [
                    'month'  =>  $this->Date_of_birth->month,
                    'day'  =>    $this->Date_of_birth->day,
                    'year'  =>   $this->Date_of_birth->year,
                ]
        ];
    }
}

当我在postman中运行代码时,出现以下错误:


简单地说,当外键字段不是它要求我在界面中输入它时,我如何存储它?

tkclm6bt

tkclm6bt1#

你能解释更多关于你的关系表吗?我认为你可以擦除你的代码with在控制器存储,如果不使用

class FamilyDetailsController extends Controller
{
    public function store(StoreFamilyDetailsRequest $request)
    {
        $familyDetails = FamilyDetails::create($request->validated());
        return new FamilyDetailsResource($familyDetails);
    }
}

相关问题