ChrisBratherton
ChrisBratherton

Reputation: 1590

Account or Contact Transactions relationship in Laravel 4.2

Ok, here is what I am trying to achieve, I have an accounts table that contains bank accounts, a transactions table that contains individual transactions, there are multiple fields in this table:

id
from
to
amount
date

The from and two fields are either a contact_id or an account_id.

For example, a user is creating a transaction to a contact from an account and vice versa

How can I set this up as a relationship in laravel?

Upvotes: 0

Views: 106

Answers (1)

TheBurgerShot
TheBurgerShot

Reputation: 1616

I don't know how familiar you are with Laravel, but you have to make a 'Transaction' model containing this:

<?php

class Transaction extends \Eloquent
{
   protected $table = 'transactions';

   public function sender(){
      return $this->belongsTo('User', 'from'); //One-to-one relationship between Transaction and User
   }

   public function receiver(){
      return $this->belongsTo('User', 'to');
   }
}

It might be more elegant to use 'sender_id' and 'receiver_id' in the transactions table, so it looks like this:

public function sender(){
   return $this->belongsTo('User', 'sender_id');
}

public function receiver(){
   return $this->belongsTo('User', 'receiver_id');
}

Upvotes: 0

Related Questions