Laravel MVC CRUD

Special Thanks to Alex Coleman for this awesome explanation and tutorial!

Laravel applications follow the traditional Model-View-Controller design pattern, where you use:

  • Controllers to handle user requests and retrieve data, by leveraging Models
  • Models to interact with your database and retrieve your objects’ information
  • Views to render pages

Additionally, routes are used to map URLs to designated controller actions, as shown below.

 

so…

  • A request is made — say, when a user enters a URL associated with your application.
  • A route associated with that URL maps the URL to a controller action.
  • That controller action leverages the necessary model(s) to retrieve information from the database, and then passes that data off to a view.
  • And that view renders the final page.

Make ‚Car‘ Model. Creates app/Car.php & database/migrations/[timestamp]_create_cars_table.php
Why use Migrations: https://selftaughtcoders.com/from-idea-to-launch/lesson-15/managing-database-with-laravel-5-migrations/

php artisan make:model Car --migration

Create cars table ([timestamp]_create_cars_table.php) with Artisan command.

php artisan migrate

In Laravel, a type of object — in this case Car — is referred to as a resource.
Since it’s so common to build applications around resources, you can generate a resource controller — a controller to handle all requests related to a resource — using Artisan command:
Creates app/Http/Controllers/CarControllers.php & all typical CRUD actions

php artisan make:controller CarController

Defining a single resource route, which creates routes for all of those resource controller actions.
Adding Route::resource('cars', 'CarController');to app/Http/routes.php

That single route definition will define all of the routes related to our Car resource:

Request Type Path Action Route Name
GET /cars index cars.index
GET /cars/create create cars.create
POST /cars store cars.store
GET /cars/{car} show cars.show
GET /cars/{car}/edit edit cars.edit
PUT/PATCH /cars/{car} update cars.update
DELETE /cars/{car} destroy cars.destroy

Add new use statement above CarController

.
use App\Car;

class CarController extends Controller
{
.

For show action put this inside

public function show($id)
    {
      $car = Car::find($id);
      return view('cars.show', array('car' => $car));
    }

Create new ressources/views/cars/show.blade.php & add following

<!DOCTYPE html>
<html>
  <head>
    <title>Car {{ $car->id }}</title>
  </head>
  <body>
    <h1>Car {{ $car->id }}</h1>
    <ul>
      <li>Make: {{ $car->make }}</li>
      <li>Model: {{ $car->model }}</li>
      <li>Produced on: {{ $car->produced_on }}</li>
    </ul>
  </body>
</html>

 

 

 

Quelle: https://selftaughtcoders.com/from-idea-to-launch/lesson-17/laravel-5-mvc-application-in-10-minutes/

No comment yet, add your voice below!


Add a Comment

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Comment *

Name *
Email *
Website

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.