Enabled console invoice generation
This commit is contained in:
37
app/Traits/PushNew.php
Normal file
37
app/Traits/PushNew.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Enables a pushnew() method, similar to push() but when all relationships are new records
|
||||
*
|
||||
* Base on Model::push()
|
||||
*/
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
trait PushNew
|
||||
{
|
||||
public function pushNew() {
|
||||
if (! $this->save()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// To sync all of the relationships to the database, we will simply spin through
|
||||
// the relationships and save each model via this "pushNew" method, which allows
|
||||
// us to recurse into all of these nested relations for the model instance.
|
||||
foreach ($this->relations as $models) {
|
||||
$models = $models instanceof Collection
|
||||
? $models->all() : [$models];
|
||||
|
||||
foreach (array_filter($models) as $model) {
|
||||
$model->setAttribute($this->getForeignKey(),$this->{$this->getKeyName()});
|
||||
|
||||
if (! $model->pushNew()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user