Insert Many To Many Laravel 5.4

Asked

Viewed 787 times

1

I have a situation as follows. I have 4 user tables -> department -> categoria_department -> category -> posts Where in my view I have a select where it shows a data relation on ex department:

inserir a descrição da imagem aqui

That information is coming from the database. I also have another select for the category see an ex:

inserir a descrição da imagem aqui

So when I go to register the Article, it has to save this information in the pivot table that in the case is categorie_departamento. This table makes Manytomany relationship with the Departments and Category tables.

In my code it’s like this:

    $departamento = new Departamento();
    $categoria = new Categoria();
    $postagem = new Postagem();
    $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();

So far so good. more it is saving the right id in the field ta pivot table right more later it writes one more line see:

inserir a descrição da imagem aqui

What can that be?

Another thing if there is an easier way to do they can say why I have never made a posting system in my life.

Table picture the last 4 table images are the ones I want

inserir a descrição da imagem aqui

  • The answer didn’t work?

1 answer

1


When you’re in a relationship many for many with the and uses the method attach to add items in the temp table do not need to pass in your specific case the code of the category key, as it is being navigated by the relations, the code itself of the already have this key and just need to pass the code of the department, it is for that of the duplication of lines, example of what would be a code, because, also has some problems in its 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();

i.e., the attach the documentation accepts a code that identifies the key of the other table. There is a sync who accepts a array simple as described in its documentation and may be useful in insertion and control of removal of the items in your intermediate table, that is, you are responsible for removing the items that do not exist in the array and are present in your table and confirms 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 Links for reading

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.