Questions & Answers

pass data to edit from the controller to the view in a many to many relationship

Hi everyone i have a many-to-many relationship between the turnos table and the dias table like this:

database schema

Currently, I'm doing the CRUD of the turnos table and for each turnos I have to assign many dias, I did it with the attach method.

Now the issue is in the edit method... how am I gonna get the assigned dias that is related to that turno so I can pass it to the view and the user can edit it?

If someone knows it please help me, I would appreciate it very much

//Dias Model
public function turnos()
    return $this->belongsToMany(Turno::class);

//Turnos Model
public function dias()
    return $this->belongsToMany(Dia::class);

// Controller
public function edit(Turno $turno)
    // $dias = ??
    return Inertia::render('Turnos/Editar', [
        'turno' => $turno,
        'dias' => ??

The edit view Should looks like this:


2023-01-21 23:01:08
If Turnos "hasMany" dias relationship is well done, you could access dias using $turno->dias to get them all.
Answers(0) :