Answer the question
In order to leave comments, you need to log in
How to bind id with hasOne?
There are company
id | name | form_id
forms
tables
id | name
If the Company model has
public function form()
{
return $this->hasOne(Form::class);
}
then how to work with it? $company = new Company();
$company->name = 'some name';
$form = Form::firstOrCreate(); // передаются данные из запроса
$company->form_id = $form->id;
form()
in which it returns HasOne
? Form
( laravel is looking for forms.company_id , which is not there), it just turned out like thispublic function form()
{
return CompanyForm::find($this->form_id)->first();
}
Answer the question
In order to leave comments, you need to log in
The logic is not clear at all. What is form and why is it so associated with company.
But if you take it as written, then the relationship should be form hasOne Company and company belongsTo form. in the form model
public function company()
{
return $this->hasOne(Company::class);
}
$form->company()->create($request->validated());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question