So what I am trying to do is produce a trial balance report. I have to get all chart of accounts(coas) and sum of its debit and sum of its credit between two dates. I have these tables in the database and these are their attributes

1. Client: id, name
2. Coas: id, name
3. Journal: id, description, date
4. Journal\_details: id, journal\_id, coa\_id, debit, credit

I have already put their relationships in the model.

**Client.php**

```
public function coas()
{

    return $this->belongsToMany('App\Coa');
}

public function journal(){

    return $this->hasMany('App\Journal');
}

```

**Coa.php**

```
public function clients(){

    return $this->belongsToMany('App\Client');
}

public function journals_details(){

    return $this->hasMany('App\JournalDetails');
}

```

**Journal.php**

```
public function journal_details(){

    return $this->hasMany('App\JournalDetails');
}

public function client(){

    return $this->belongsTo('App\Client');
}

```

**JournalDetails.php**

```
public function journal(){

    return $this->belongsTo('App\Journal');
}

public function coa()
{
    return $this->belongsTo('App\Coa');
}

```

I'm trying to get all of the coas with journal details of a specific client, which I am already doing.
 $trials = $client->coas()->with('journals\_details')->get();

However, I am using a date range to select only those that belonged to specific date that is inputted. Here is my controller. I tried this but it doesn't work.

```
public function trial_balance_generate(Request $request)
        {

            $client = Client::find($request->client_id);

            $start = \Carbon\Carbon::parse($request->from)->startOfDay();  

            $end = \Carbon\Carbon::parse($request->to)->endOfDay();

            $data= $client->coas()->with('journals_details')->whereBetween('date',[$start,$end])->get();

            return response()->json($data);
        }

```

I know there is something wrong with how I get the data. I just don't know how to get all the coas and its details with the journal header that contains the date. 

This is my javascript for getting the date range.

```
$('.date').on('change', function() {
        var from = $('#from').val();
        var to = $('#to').val();
        var client_id = $('.clientHidden').val();
        $.ajax({
                type    : 'get',
                url     : '/user/'+client_id+'/reports/trialbalance/generate/',
                dataType: 'json',
                data    : {
                    'from':from,
                    'to':to,
                    client_id':client_id
                    },
                success:function(data){
                    $('td').remove();
                    for(var ctr = 0; ctr < data.length; ctr++)
                    {

                        $('#reportTbody').append()

                                '<tr><td>'+ data[ctr].name +'</td><td>{{$trial->journals_details->sum("debit")}}</td><td>{{$trial->journals_details->sum("credit")}}</td></tr>'+

                    }                
            }           
        });
});

```

Here is a photo of what I am trying to achieve <https://imgur.com/a/IZvio> That returns everything so no dates yet.