I am trying to insert into 2 different tables(House and Contact) using a single method.
public function store(HouseRequest $request){
$dataForm = $request->all();
$house = House::create($dataForm); //Insert into House table
$contact = $house->contact()->create($dataForm['contact']); //Insert into Contact table
return back('dashboard/houses')->with('message', 'Success');
}
Here is my table Contact:
Schema::create('contacts', function (Blueprint $table) {
$table->integer('house_id')->unsigned();
$table->string('name', 255);
$table->char('phone', 11);
$table->timestamps();
$table->foreign('house_id')->references('id')->on('houses')->onDelete('cascade')
}
House Model:
public function contact(){
return $this->hasOne(Contact::class);
}
It works fine (inserts into both tables), but after submitting the form, I got this:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from contacts
where id
= 0 limit 1)
I don't know why it is running the query above. How can I get rid of this error?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire