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

i_minku's avatar

Fetch data in model class, Pass to controller class, Then pass to view

I just created a table students using make:migration and then migrate to database. I inserted data in students table manually.

Now i want to retrieve that data in laravel using mvc pattern.

I have 1 controller StudentController, 1 Model class Student, 1 view test.blade.php

I also defined route in web.php as follow Route::get('test', 'StudentsController@retrievedata');

StudentController as follow

0 likes
3 replies
Dry7's avatar
class StudentsController extends Controller {
    public function retrievedata()
    {
        return view('test', [
            'students' => Student::all()
        ]);
    }
}

and do not forget to connect the student model in the use block

i_minku's avatar

I got your point. But i also want to know why we need model if we can Fetch data using DB class as below code

class StudentsController extends Controller { public function retrievedata() { return view('test', [ 'students' => DB::table('students')->get(['*']); ]); } }

Dhaval_patel's avatar

Hey, @i_minku whatever @Dry7 has mentioned is the main-feature of laravel called eloquent. Your way (using Query builder) is also correct though.

Please or to participate in this conversation.