I have a situation as follows. I have 4 tables users - > department - > category_department - > category - > postings Where in my view I have a select where it shows a data relation about ex department:
Thisinformationalreadycomesfromthedatabase.Ialsohaveanotherselectforthecategoryseeanex:
SowhenIgotoregisterthearticle,ithastosavethisinformationinthepivotable,whichinthiscaseiscategory_department.ThistablemakesManyToManyrelationshipwithDepartmentsandCategorytables.
Inmycodeitlookslikethis:
$departamento=newDepartamento();$categoria=newCategoria();$postagem=newPostagem();$findCat=$categoria->where('id',$request->categoria)->get()->first();$findDep=$departamento->where('id',$request->departamento)->get()->first();$findCat->departamentos()->attach(['departamento_id'=>$findDep->id,'categoria_id'=>$findCat->id,]);$postagem->categoria_id=$request->categoria;$postagem->titulo=$request->titulo;$postagem->imagem=$filename;$postagem->descricao=$request->descricao;$postagem->status=$request->status;$result=$postagem->save();
Sofarsogood.butitissavingtherightidinthetablefieldpivorightmorethenitwritesonemorelinesee:
Whatcanthisbe?
AnotherthingifthereisaneasierwaytodoitcantellwhyI'veneverdoneapostingsysteminmylife.
Pictureofthetablethelast4imagesofthetablearetheonesIwant