-
app/Models/Transactions/Transaction.php
Open in GitHubuse Illuminate\Database\Eloquent\Model; class Transaction extends Model { // public function tags() { return $this->belongsToMany('App\Models\Tags\Tag', 'transactions_tags', 'transaction_id', 'tag_id') ->withTimestamps(); } // }
-
app/Services/Transactions/AddTransaction.php
Open in GitHubuse App\Models\Transactions\Transaction; use App\Models\Accounts\CheckingAccount; use App\Models\Accounts\CreditCard; use App\Services\Tags\GetTag; class AddTransaction { // public function store() { $transaction = new Transaction(); $transaction->unique_id = uniqid(); $transaction->user_id = $this->user->id; $transaction->accountable_id = $this->account->id; $transaction->accountable_type = get_class( $this->account ); $transaction->amount = $this->amount; $transaction->date = $this->date; $transaction->name = $this->name; $transaction->description = $this->description; $transaction->direction = $this->direction; $transaction->reconciled = 0; $transaction->save(); $this->tagTransaction( $transaction ); return $transaction; } private function tagTransaction( $transaction ) { if( $this->tags ){ $syncTags = []; foreach( $this->tags as $submittedTag ){ $getTag = new GetTag( $submittedTag->name, $this->user ); $tag = $getTag->get(); array_push( $syncTags, $tag->id ); } $transaction->tags()->sync( $syncTags ); } } // }