Hi everyone! Hope you doing well...
In a many to many relation between documents and transactions, I have a pivot model that has id and extra field amount along with relation columns transaction_id and document_id.
You can see model definitions below, they're basic but notice that my document and transaction tables have their own amount column which are different than pivot's amount.
The problem is when I call $document->transactions or vice versa, it returns pivot's id and pivot's amount value instead of model id and amount [along with other attributes of the model].
Shouldn't I only get the models attributes and access the pivot attributes by calling $transaction->pivot?
If I change pivot column names to something like pivot_id and pivot_amount everything is ok so...
Thanks in advance ;)
Here are the model definitions...
DocumentTransaction.php
class DocumentTransaction extends Pivot
{
use SoftDeletes, AuthorStamps;
//columns: id, transaction_id, document_id, amount, created_at, deleted_at, created_by, deleted_by,...
public $incrementing = true;
protected $table = 'document_transaction';
protected $dates = ['deleted_at'];
public function document(): Relation
{
return $this->belongsTo(Document::class);
}
public function transaction(): Relation
{
return $this->belongsTo(Transaction::class);
}
}
Document.php
class Document extends Model
{
//columns: id, amount,...
public function transactions()
{
return $this->belongsToMany(Transaction::class)
->using(DocumentTransaction::class)
->wherePivot('deleted_at', null)
->withPivot(['amount'])
->withTimestamps();
}
}
Transaction.php
class Transaction extends Model
{
//columns: id, amount,...
public function documents()
{
return $this->belongsToMany(Document::class)
->using(DocumentTransaction::class)
->wherePivot('deleted_at', null)
->withPivot(['amount'])
->withTimestamps();
}
}