Good day. People.
Tell me who knows how model :: updateorcreate ()
in Laravel (4.2)
Answer 1, Authority 100%
Attempts to find the first entry based on the transmitted $ attributes
if it won’t find – create. Then fills the data from $ Values
and saves.
Example of use:
$ Book = Book :: Updateorcreate (
['isbn' = & gt; '9780451157485'],
['title' = & gt; 'Atlas Shrugged']);
If there are no books with the specified ISBN, it will be. In any case, Title
will be as indicated.
Source:
// vendor / laravel / framework / src / illuminate / database / eloquent / model.php: 553,570
/ **
* Create or Update A Record Matching The Attributes, And Fill It With Values.
*
* @param Array $ attributes
* @param Array $ Values
* @return static.
* /
Public Static Function Updateorcreate (Array $ Attributes, Array $ Values = Array ())
{
$ instance = static :: firstnew ($ attributes);
$ instance- & gt; fill ($ values) - & gt; save ();
Return $ instance;
}
/ **
* Get The First Record Matching The Attributes or Instantiate It.
*
* @param Array $ attributes
* @return static.
* /
Public Static Function FirstNew (Array $ attributes)
{
if (! is_null ($ instance = static :: where ($ attributes) - & gt; first ()))
{
Return $ instance;
}
RETURN NEW STATIC ($ attributes);
}