Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add model relationship without save

I have the next relationship User hasMany Contacts. In normal situation is use $user->contacts()->save($contact) to add and save a contact to the user, but I need to associate contacts to users without save the models (User and Contacts).

edit:

I need to build a plant that receives one of such methods and return an XML a collection of templates, only in some of these models will be stored at postiriori.

like image 702
Miguel Borges Avatar asked Nov 03 '13 04:11

Miguel Borges


1 Answers

You can use the associate on your model with belongsTo.

$contact= Contact::find(10);

$user->account()->associate($contact);

$user->save(); // You do need to update your user

Source: http://laravel.com/docs/eloquent

like image 156
JofryHS Avatar answered Sep 20 '22 16:09

JofryHS