Create and edit data from a table with Laravel relationship

Asked

Viewed 643 times

4

I have a table called agenda, among the fields of this agenda it is possible to add several responsible for it, which are the users registered in the system, for this there is a field of type multiple select, which allows you to select multiple user Ids, and to save these various "responsible", I created a table called agenda_responsaveis, that saves agenda id (agenda_id) and the id of each user (user_id).

Currently, when creating the schedule, to save the responsible ones, I traverse the data of select with a foreach and insert the data in the table agenda_responsaveis

Ex:

$create = Agenda::create($data);

        foreach ($request->responsavel_id as $key => $id) {

            AgendaResponsavel::create(array(
                'agenda_id' => $create->
                'user_id' => $id
            ));
        }

And in the editing part of the agenda, I search all the records with the agenda_id, I delete the old data and re-enter the new data coming from select. I do this because I don’t have the id of each record in the table agenda_responsaveis.

Ex:

AgendaResponsavel::where('agenda_id', $agenda_id)->delete();

foreach ($request->responsavel_id as $key => $id) {

            AgendaResponsavel::create(array(
                'agenda_id' => $agenda_id,
                'user_id' => $id
            ));
        }

would you like to know if there is a more efficient way to do this? without seeming a "gambiarra"

2 answers

1

As friend Jedson Melo said you can use the Eloquent which assists in the process of creating relationships in databases. According to the documentation models can relate to others by abstracting the relationship from fact. As identified it is a relationship N*N, or many for many, which generates a pivot table. At the Laravel I can represent this relationship in a simpler way. Within your Agenda model you should create a function (usually this one uses the name of the other relationship model in the plural). It would look like this:

public function responsaveis(){
return $this->belongsToMany('App\User', 'agenda_responsaveis', 'agenda_id', 'user_id');
}

The first argument is the path of the other relationship model, the second(optional) is the table name, it is optional because if you use the standard Laravel is not necessary to pass it because the Laravel already identifies. The two others are also optional and only refer to the name of the fields. As your case the names differ a little from the pattern, it is good to pass. Ai to insert in the pivot table there are two possibilities:

  • Using the method attach
  • Or using the method sync

The attach simply associates at the base. That in the case is your first need. Already the sync serves your second problem. What it does, every time it’s called, is delete the previous relationships and keep the most current ones you’ve ever sent. It’s something like a synchronization. So taking the example of the friend, you could use the attach at the time of insertion:

//caso os ids estejam num array, você pode envia-los todos de uma vez
$agenda = Agenda::create($data);
$agenda->responsaveis()->attach($array_de_ids_responsaveis);

Ai for editing you would do the search and then use Sync:

$agenda = Agenda::find($id_agenda);
$agenda->responsaveis()->sync($novos_ids);

I think that’s it, any doubt refer to documentation.

  • You need to have the relationship on the App User as well?

  • It is not necessary. But if you want to return the schedules or insert in the pivot table from the User ai model you create. Let’s say you want to take the schedules of a responsible person: $user = User:find($id);
$agendas = $user->agendas;
. Anyway, if you don’t need to manipulate the data through the other model, you don’t need to create.

0

Using the relationships that the ORM Eloquent provides. In your case you have a relationship of Many to Many for between schedule and responsible, so just set up the models by following the Documentation and you can use something like this:

$agenda = Agenda::create($data);
$agenda->responsaveis()->attach($request->responsavel_id);

Browser other questions tagged

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