Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

priyasanka00's avatar

Sort a variable data by created at date

Hy i want to sort my variable data in my view as most resent database data. how can i do this pl help,

@foreach($form1 as $form)

                <div class="col-sm">
                    </br>
                    <div class="card" style="width: 18rem;">
                        img src="{{ $form->image }}" class="card-img-top" alt="..."
                        <div class="card-body">
                            <h5 class="card-title">{{$form->heading}}</h5>
                            <p class="card-text">{{$form->text}}</p>
                            <a href="/viewformdata/{{$form->id}}" class="btn btn-primary"> See              
                                                           More</a>
                        </div>
                    </div>
                </div>

@endforeach

0 likes
17 replies
bobbybouwmann's avatar

You have to do that in your controller. So something like this

public function index()
{
    $forms = Form::latest()->get();

    return view('forms.index', compact('forms'));
}
priyasanka00's avatar

hy dear this is my controller,

    //dd($form1);
    $dbreturn=form1::all();
    //dd($dbreturn);

    return view('home1')->with('form1',$dbreturn);
priyasanka00's avatar

and this is my route:

Route::get('/home1', function () { $data=App\form1::all(); return view('home1')->with('form1',$data);

Snapey's avatar

oh, not even using controller then?

Answer is the same, but really you need to do some more studying...

priyasanka00's avatar

$dbreturn=form1::latest()->get(); //dd($dbreturn);

    return view('home1')->with('form1',$dbreturn);

i did it bu doesnt change anything

Snapey's avatar

in the controller or in the route? Which code are you using?

Sinnbeck's avatar

Does it change anything if you use oldest instead of latest?

priyasanka00's avatar

@snapey @bobbybouwmann Thanks both of you, i got the point and edited correctly like this my route also:

Route::get('/home1', function () { $data=App\form1::latest()->get() return view('home1')->with('form1',$data)}

Sinnbeck's avatar

Uhh how is my response the best? I simply told you to reread the reply from @bobbybouwmann

Please change it

Edit: I removed my answer as it simply said the same as had already been said

seen's avatar

The easiest way ^_^

2 likes

Please or to participate in this conversation.