Question: I am using a hasMany in my Model class to retrive the clients notes, how ever i want to order these notes by the latest date created in laravel blade template. 

My code is below and im getting an error on this.

Please advice me..

```
@foreach($clients->notes->orderBy('created_at', 'desc') as $note)
    <table class="table table-bordered">
        <tr>
            <td class="col-xs-2 col-md-2"><b>Created On:</b> {{ date('d/m/y', strtotime($note->created_at)) }} <b>&#64;</b> {{ date('g:i A', strtotime($note->created_at)) }} </td>
            <td class="col-xs-14 col-md-12">{{ $note->notes }}</td>
        </tr>
    </table>
@endforeach

```

Answer: 
Guessing, because you haven't told us *what* error you're getting, but:

`$clients->notes` is an already-fetched collection of results. `$clients->notes()` is a query builder that you can apply further logic like ordering or additional criteria to.

You likely want:

```
$clients->notes()->orderBy('created_at', 'desc')->get()

```

but you should do that in the controller and pass it to the view instead of having the query directly in the Blade template.

(You can alternatively use Laravel's collection functions on `$clients->notes`, including [the `sortBy()` function](https://laravel.com/docs/5.2/collections#method-sortby)).
Data must be ordered within controller or models. If you have used hasMany validation in model you can do as mentioned below

```
In model write association 
    public function notes()
    {
        return $this->hasMany('Note')->orderBy('created_at', 'desc');
    }

    In your controller function associate client with notes like this 

    $clients = Client::with('notes')->get();
    Hope you get your answer

```