Laravel — P40: Controller Index Method (CMP)
11 min readJan 24, 2023
The controller architecture and the routes are setup and ready for us to start tackling. The first one on the list is index. We’re going to pull all of the data out of the table, put it into a view, and return it back to the user.
The Route and the Controller Method
We currently have the following code already created.
Route::prefix('/personalcars')->group(function() {
Route::get('/', [PersonalCarController::class, 'index']);
// ...
});
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PersonalCarController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// get data from model
// send data to view
// return view to user
}
// ...
}
We don’t have to make the modification on our route file. We simply need to work on our controller and create a new view.