1

I have a table called transactions and another table called cars with the below structure:

transactions

| id  | date | amount | status | user_added |
| --- | ---- | ------ | ------ | ---------- |

cars

| id  | plate | specs | buy_transaction  | sell_transaction  |
| --- | ----- | ----- | ---------------- | ----------------- |

A car has always a buy_transaction but not always a sell_transaction, situation is that I am trying to get all transactions (that might be car-related or not car-related) and include the CAR related to that transaction weather it is sold or bought, so I need to make the relationship conditional but i couldn't achieve that.

$journal = Transaction::with(
    ['user'=> function($query) { 
        $query->select('id', 'name');
     },
     'income',
     'outcome',
     'car'
    ])->where('date', '>=', $fromDate)->where('date', '<=', $toDate);

This is the modal class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Transaction extends Model
{

    public function income()
    {
        //.....
    }

    public function outcome()
    {
        //.....
    }

    public function user()
    {
        return $this->belongsTo('App\User', 'user_added', 'id');
    }

    // *** problem starts here ***

    public function car()
    {
        if (transaction status == 1) {

        return $this->belongsTo('App\Car', 'id', 'sell_transaction');

        }
        else if (transaction status == 2) {

        return $this->belongsTo('App\Car', 'id', 'buy_transaction');

        }
    }

}

I need to stick to that query structure because the query command is longer and I am joining and including other tables, I was hoping I could make the car() belongsTo relation conditional somehow.

I followed some similar situations like this but it didn't work for me.

Thank you.

tinyCoder
  • 350
  • 13
  • 37
  • I guess you should split Transaction:car() method for two different methods: Transaction:soldCar() and Transaction:boughtCar(). – Vasyl Sovyak Mar 29 '18 at 09:14
  • Thanks, I tried that already but I can't, because later I will use the result returned as a jSON array and it must be under `car` node, no matter it was sold or bought. – tinyCoder Mar 29 '18 at 09:16

1 Answers1

0

The link you post has the answer

public function soldCar()
{
   return $this->belongsTo('App\Car', 'id', 'sell_transaction');
}
public function boughtCar()
{
   return $this->belongsTo('App\Car', 'id', 'buy_transaction');
}
public function scopeCar($query)
{   
    return $query->when($this->type === '1',function($q){
       return $q->with('soldCar');
    })
    ->when($this->type === '2',function($q){
       return $q->with('boughtCar');
    });

}

Edit: Also, this looks like a prime example for polymorfic relations as documented here https://laravel.com/docs/5.5/eloquent-relationships#polymorphic-relations

Sérgio Reis
  • 2,483
  • 2
  • 19
  • 32
  • I tried it already, I got an error `Undefined variable: query`, and when i use `scopeCar($query)` I get another BadMethodCallException error `Call to undefined method Illuminate\Database\Query\Builder::addEagerConstraints()`. – tinyCoder Mar 29 '18 at 09:21
  • I edited my answer, forgot to recieve `$query` on the score function – Sérgio Reis Mar 29 '18 at 09:23
  • I just mentioned above that i corrected your answer to `scopeCar($query)` but I got the other error, also `type` need to be `status`. – tinyCoder Mar 29 '18 at 09:28
  • if the status is integer, remove the `'` from the when conditions – Sérgio Reis Mar 29 '18 at 09:31
  • No it is a string, thank you :( `Call to undefined method Illuminate\Database\Query\Builder::addEagerConstraints()` – tinyCoder Mar 29 '18 at 09:32
  • Similar situation here i think: https://stackoverflow.com/questions/39476928 but I want to get the result as `car` not `soldCar` or `boughtCar`, is that possible? – tinyCoder Mar 29 '18 at 09:37