vendredi 29 décembre 2017

laravel - Updating relation with many 'childs'

I have this database:
parents

| id |  name  |  
+----+--------+  
|  1 |  Paul  |  
|  2 |  Annet |

childs

| id |    name   |  
+----+-----------+  
|  1 |  Micheal  |  
|  2 |   Susan   |

and pivot table parents_childs

| parent_id | child_id | custom_field_1 | custom_field_2 |  
+-----------+----------+----------------+----------------+  
|     1     |     1    |    value_1     |      (null)    |  
|     2     |     1    |  value_another |     value_3    |

and standard relation belongsToMany

public function parents(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
    return $this->belongsToMany('\App\Parent', 'parents_childs', 'child_id', 'parent_id')
                ->withPivot(
                    'custom_field_1',
                    'custom_field_2'
                );
}

Now I need to update pivot fields of specified child but only for one parent, eg.

SET red_value FOR custom_field_2 WHERE child_id = 1 AND parent_id = 2

How can I do this without QueryBuilder?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire