Insert Many To Many Laravel 5.4

1

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

    
asked by anonymous 12.09.2017 / 01:12

1 answer

1

When you have a many relationship for many with and uses the attach method to add items to the itermediary table does not need to pass in its specific case the code of the category key, as it is being navigated by the relations itself code already have this key and just need to pass the code department, this is why the duplication of lines, example of what a code would be, because, you also have some problems in your primary code :

$departamento = new Departamento();
$categoria = new Categoria();
$postagem = new Postagem();

$findCat = $categoria->where('id',$request->categoria)
                     ->first();
$findDep = $departamento->where('id',$request->departamento)
                     ->first();

if ($findCat && $findDep) // se os dois existem
{
    $findCat->departamentos()
        ->attach($findDep); // adiciona um item a solução
}

$postagem->categoria_id = $request->categoria;
$postagem->titulo = $request->titulo;
$postagem->imagem = $filename;
$postagem->descricao = $request->descricao;
$postagem->status = $request->status;
$result = $postagem->save();

that is, the attach through the documentation accepts a code that identifies the key of the other table. There is sync that accepts a% simple% as described in its documentation and can be useful in the insertion and control of removal of the items in your middle table, remove items that do not exist in array and are present in your table and confirm those that do not exist and are present in array , example :

if ($findCat && $findDep) // se os dois existem
{
    $findCat->departamentos()
        ->attach([1,2,3,4]); // sincroniza os itens
}
Post and Reading Links

12.09.2017 / 15:56