Streamlining Laravel: Routes and Controllers High-Level Overview
Now that our Model relationships are defined, we can start setting up the initial infrastructure for our routes and controllers. We’ll list out what kind of data will be sent, what we’re expecting, and then we’ll worry about creating views in the next article.
https://medium.com/geekculture/laravel-p38-many-to-many-relationship-cmp-bd8847de9114
Brainstorming
I’ve purposefully not created any of this beforehand. I’m writing this code as it seems natural to me. What I want to do is brainstorm on what we need to create next.
We know that we have the following models:
PersonalCarBrand
PersonalCarModel
PersonalCar
Image
The question is, do we want to have CRUD (create, read, update, delete) operations on each of these models? I believe that this is preference, but I think that we should to a certain extent. Let’s look at each of these on their own.
PersonalCarBrand
For this application, I don’t want to manage my own car brands. I don’t need to see just the brands. I don’t need to edit just the brands (or at least I don’t think so yet). I don’t need to delete just the brands. What I mean by that is that I don’t need to go to a page and only see the brands inside of the personal_car_brands
table. I just want to see my cars. How the database handles it is up to it.
PersonalCarModel
Same type of reasoning. I don’t want to manage my own models. The PersonalCarModel
allows me to insert the information into the table. I don’t need to create a route or a controller for it.
Image
This is another one. I’m not trying to build a gallery that shows all of my images. I just want to see the images associated with each vehicle.
PersonalCar
And then we get to the PersonalCar
. This model will need a controller and will need routes associated with it. That means that we’ll need routes and we’ll need a controller. Let’s get the controller out of the way.
# php artisan make:controller PersonalCarController
INFO Controller [app/Http/Controllers/PersonalCarController.php] created successfully.
#
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PersonalCarController extends Controller
{
//
}
To recap our introduction, we need to have the following views and functionality:
Index: Our main page will display all of our cars and links to each individual car.
We need a route that sends a request to the index
method in our PersonalCarController
. The index
method should utilize our PersonalCar
model to retrieve the data from our personal_cars
table and send it to our personalcar/index.blade.php
view. Once the view does its thing, the controller will send the data back to our user through our route.
The route is pretty straightforward. It should just be a get
request to /personalcars
.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PersonalCarController extends Controller
{
public function index()
{
// get data from model
// send data to view
// return view to user
}
}
Show: When we visit a specific vehicle, we’ll be able to view all of its details on the show page. We’ll also be able to click to edit it or delete it.
The show
route should be created. It will send an id
to the show
method in our controller. The show
method will use the PersonalCar
model to get the specific resource out of our table. It will send that data to our personalcar/show.blade.php
view. The computed file will be returned to the controller and the controller will return it to the user through our route.
The route needs to send our resource id, which means that it needs to be a get
request with id
as an argument: /personalcars/{PersonalCar}
(i.e. /personalcars/1
).
public function show($id)
{
// Look up resource with id
// Send data to show view
// Return view to user through route
}
Edit/Update: Once we click on the edit button, the text will transform into a pre-populated field for that particular car. We can make modifications and click Save. Clicking the Save button will trigger the data to be sent to the backend and the information updated for that particular car.
This contains two routes and two controller methods. One for displaying the form and the other one for grabbing the data from the form and updating the resource. The get
request to /personalcars/{PersonalCar}/edit
will return the form for that particular vehicle with data already pre-filled (i.e. /personalcars/1/edit
). Once you edit the details in the form and click update, it will send a put
request to /personalcars/{PersonalCar}
(i.e. /personalcars/1
) and the update
route will be able to update the information.
public function edit($id)
{
// get resource with the help of the PersonalCar model
// send data to edit view
// the view injects the data into the form
// the view is returned to the user through the route
}
public function update(Request $request, $id)
{
// get data from form
// update the resource with the form data
// return a success message
}
Delete: We’ll also need to be able to delete a specific car. Once we click delete, Laravel will process the deletion for us.
To delete a post, the delete
request is sent to /personalcars/{PersonalCar}
(i.e. /personalcars/1
). The route calls the destroy
method in our controller and passes the resource’s id
to it. The destroy
method removes the resource from the table.
public function destroy($id)
{
// delete the record from the table
// return redirect with success message
}
Insert: Finally, we need to have the ability to insert a new car into the system. A form needs to be displayed where the user can enter data, and once they click Submit, the data will be processed and inserted.
This is another one that needs two methods. One route, a get
request to /personalcars/create
will return a blank form for the user to fill out. Once the user clicks submit, a post
request will be sent to /personalcars
with all of the form data. The store
method will handle storing the data and the create
method will handle showing a blank form.
public function create()
{
// Return the form to the user
}
public function store(Request $request)
{
// get data from form
// validate data
// insert data into table
// return message that insertion was successful
}
When we really look at it, what we just created is a Resource Controller. We can do this differently, and we will, when we tackle resource controllers later. The routes that we need to create are:
/personalcars (GET)
/personalcars/create (GET)
/personalcars (POST)
/personalcars/{PersonalCar} (GET)
/personalcars/{PersonalCar}/edit (GET)
/personalcars/{PersonalCar} (PUT)
/personalcars/{PersonalCar} (DELETE)
Our PersonalCarController
looks like this now.
<?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
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
// Return the form to the user
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// get data from form
// validate data
// insert data into table
// return message that insertion was successful
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
// Look up resource with id
// Send data to show view
// Return view to user through route
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
// get resource with the help of the PersonalCar model
// send data to edit view
// the view injects the data into the form
// the view is returned to the user through the route
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
// get data from form
// update the resource with the form data
// return a success message
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
// delete the record from the table
// return redirect with success message
}
}
Our routes file looks like this for now. Like I said, since this is a resource controller there is a way to simplify this, but we won’t do that until we cover resource controllers.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PersonalCarController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::prefix('/personalcars')->group(function() {
Route::get('/', [PersonalCarController::class, 'index']);
Route::get('/create', [PersonalCarController::class, 'create']);
Route::post('/', [PersonalCarController::class, 'store']);
Route::get('/{id}', [PersonalCarController::class, 'show']);
Route::get('/{id}/edit', [PersonalCarController::class, 'edit']);
Route::put('/{id}', [PersonalCarController::class, 'update']);
Route::delete('/{id}', [PersonalCarController::class, 'destroy']);
});
See you in the next article when we start looking at the one that most individuals are scared of, insertion of data.
Laravel Series
Continue your Laravel Learning.
Laravel Relationships Unlocked: Using belongsToMany, attach, and detach
Laravel – P38: CMP – Many To Many Relationship Belongstomany, Attach, Detach
Learn how to manage many-to-many relationships in Laravel using belongsToMany, attach, and detach. Simplify complex data links in your applications.
Streamlining Laravel: Routes and Controllers High-Level Overview.
Laravel – P39: CMP – Routes And Controllers High-Level Setup
Discover how to efficiently set up routes and controllers in Laravel. A high-level guide to streamline your application’s foundational structure.
Laravel Index Method: A Guide to Controller Essentials
Laravel – P40: CMP – Controller Index
Explore the role of the Index Method in Laravel controllers. Learn how to effectively utilize this method to manage and display data in your application.