Laravel Tutorial

DAY 2

Route

URL Named Route

The route helper may be used to generate URLs to named routes.

Named routes allow you to generate URLs without being coupled to the actual URL defined on the route.

Therefore, if the route’s URL changes, no changes need to be made to your route function calls.

Routing - Group and Named Route

All Laravel routes are defined in your route files, which are located in the routes directory. For more information, click here.

Routing - Group and Named Route

php artisan route:list

Routing - Named Route

Named route with model binding

Model

  • Model used to define fillable attributes.
  • Model also can be used to define relationship
  • Model also can be used to to specify custom table.
  • Model also can be used to define mutator
  • Model also can be used to define local scope

Relationship

Relationship 1:M

  1. A one-to-many relationship is used to define relationships where a single model owns any amount of other models.
  2. User has many schedules
  3. Schedule belongs to User
On Schedule.php
On User.php

Relationship - Lazy Load Attribute

A one-to-many relationship is used to define relationships where a single model owns any amount of other models

Show, Edit, Update & Delete

Views

Views are stored in the resources/views directory

  1. Go to resources/views

  2. Create trainings folder, create

    1. show.blade.php
    2. edit.blade.php

Controller

php artisan make:controller ScheduleController

7 method in controllers

Show

Controller – Show

DescriptionURLController FunctionView File
Show one of the schedulesGET/schedules/{id}show()resources/views/schedules/show.blade.php

Edit

Controller – Edit 

DescriptionURLController FunctionView File
Pull a schedule form the database and allow editingGET/schedules/{id}/editedit()resources/views/schedules/edit.blade.php

View – Edit View 

Views are stored in the resources/views directory

  1. Go to resources/views/schedules/edit.blade.php

Update

Controller – Update 

DescriptionURLController FunctionView File
Process the create form submit and save the training to the databasePOST/schedules/{id}update()NONE

Delete

Controller – Destroy/Delete

DescriptionURLController FunctionView File
Delete the training from DBGET/schedules/{id}/deletedelete()NONE

Middleware

Middleware provide a convenient mechanism for filtering HTTP requests entering your application.

For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen.

Pagination

The paginate method automatically takes care of setting the proper limit and offset based on the current page being viewed by the user.

app/Http/Controllers/ScheduleController.php

resources/views/schedules/index.blade.php

Laravel Error Page

Custom HTTP Error Pages

  1. Create folder name errors in resources/views/

  2. Example Http Error 404, create 404.blade.php

Scroll to Top