Laravel Level 1 The Laravel Basic By Spicydog (7-11-2016)
Agenda ● Prerequisites ● Directory Structure ● Route ● View ○ Blade ● Model ○ Migration ○ Eloquence ● Controller ○ Resource Controller ● Workshop & Assignments
● PHP-CLI ● LAMP Server ● Installed Laravel ● PHP IDE Prerequisites
These are what you must know today ● app ● app/Http ● app/Http/Controllers ● app/*.php ● config ● database ● public ● public/index.php ● resources ● resources/views ● routes ● routes/web.php Laravel: Directory Structure https://laravel.com/docs/5.3/structure
Laravel: Route ● Route control what URL on what to do ● We are mainly use routes/web.php ● For get request at URI / do return view name welcome ● Common HTTP Methods: GET, POST, PUT, PATCH, DELETE 1. Route::get('/', function () { 2. return view('welcome'); 3. });
Laravel: View ● View is a HTML template to display to users ● View are stored in resources/views/**/*.php ● To call view, use the function view('VIEW_PATH.VIEW_NAME', [PARAM]); ○ VIEW_PATH is directory to the view ○ VIEW_NAME is your view name, exclude .php and .blade.php ● Laravel have a template helper called blade, gonna talk later ● Now, let’s observe resources/views/welcome.blade.php together!
Laravel: Blade Template ● Blade Template is a template helper in Laravel ● Design for easy to use and elegant code ● Here are the common commands we use a lot => ● Blade template files must have .blade.php extension Layout @include('VIEW') @yield('SECTION') @extends('LAYOUT_VIEW') @section('SECTION'), @endsection Echo {{ ECHO_VARIABLE_WITH_ESCAPING_STRING }} @{{ ECHO_RAW_STRING }} {!! ECHO_VARIABLE_WITHOUT_ESCAPING_STRING !!} Control @if(), @elseif(), @else, @endif @for(), @endfor @foreach(), @endforeach Explore Example run php artisan make:auth and check your views directory
Laravel: Model ● Model is where we control Database Logic ● Model files are store in app/*.php ● Model file names is Database Table Name ● We rarely use SQL in Laravel, the data record to becomes object ● We should already have app/User.php from the php artisan make:auth ● To make new model, we run php artisan make:model ModelName
Laravel: Migration ● Migration is a version control for database schema for Laravel ● This helps you create database schema via PHP Syntax ● Go check your database/migrations/*.php files ● There are function up() and down() in each migration file ● up() indicates what to do in forward ● down() indicates what to do in backward ● We have a lot of migration commands ● Setup your database configuration in .env file ● Run php artisan migrate ● Check your database schema migrate migrate:install Create the migration repository migrate:refresh Reset and re-run all migrations migrate:reset Rollback all database migrations migrate:rollback Rollback the last database migration migrate:status Show the status of each migration
Laravel: Eloquent ● Eloquent is an ORM (Object-relational mapping) helps you manage database query via OOP ● 1-to-1, 1-to-many, many-to-many are also here! ● This allows us to do e.g. user->comments()->get(); to get all comments for a certain user ● Make a new record? $newRecord = new ModelName([“FIELD”=>”DATA”]); $newRecord->save(); ● Too many things to discuss here, have a look at https://laravel.com/docs/5.3/eloquent
Laravel: Controller ● Controller is your main logic for a certain request ● Laravel help controller to be super small compare to other frameworks ● The controller files are in app/Http/Controllers/**/*.php ● Make a new controller? php artisan make:controller NameController Let’s Explore Controllers ● Open app/Http/Controllers/HomeController.php ● Open routes/web.php ● Route::get('/home', 'HomeController@index'); tells laravel to run HomeController method index in /home
Laravel: Resource Controller ● As I said, Laravel is designed for productivity, it assumes that controller would done CRUD tasks, so it has resource controller for this. ● The default functions: index, create, store, show, edit, update, destroy ● Make a new resource controller? php artisan make:controller --resource NameController ● Map route to resource controller? Route::resource('/name, 'NameController'); ● Run php artisan route:list
Workshop & Assignment Develop a web blog with comments with mocked user data *Hint - Getting a request from HTML form using $request from post route

Laravel Level 1 (The Basic)

  • 1.
    Laravel Level 1 TheLaravel Basic By Spicydog (7-11-2016)
  • 2.
    Agenda ● Prerequisites ● DirectoryStructure ● Route ● View ○ Blade ● Model ○ Migration ○ Eloquence ● Controller ○ Resource Controller ● Workshop & Assignments
  • 3.
    ● PHP-CLI ● LAMPServer ● Installed Laravel ● PHP IDE Prerequisites
  • 4.
    These are whatyou must know today ● app ● app/Http ● app/Http/Controllers ● app/*.php ● config ● database ● public ● public/index.php ● resources ● resources/views ● routes ● routes/web.php Laravel: Directory Structure https://laravel.com/docs/5.3/structure
  • 5.
    Laravel: Route ● Routecontrol what URL on what to do ● We are mainly use routes/web.php ● For get request at URI / do return view name welcome ● Common HTTP Methods: GET, POST, PUT, PATCH, DELETE 1. Route::get('/', function () { 2. return view('welcome'); 3. });
  • 6.
    Laravel: View ● Viewis a HTML template to display to users ● View are stored in resources/views/**/*.php ● To call view, use the function view('VIEW_PATH.VIEW_NAME', [PARAM]); ○ VIEW_PATH is directory to the view ○ VIEW_NAME is your view name, exclude .php and .blade.php ● Laravel have a template helper called blade, gonna talk later ● Now, let’s observe resources/views/welcome.blade.php together!
  • 7.
    Laravel: Blade Template ●Blade Template is a template helper in Laravel ● Design for easy to use and elegant code ● Here are the common commands we use a lot => ● Blade template files must have .blade.php extension Layout @include('VIEW') @yield('SECTION') @extends('LAYOUT_VIEW') @section('SECTION'), @endsection Echo {{ ECHO_VARIABLE_WITH_ESCAPING_STRING }} @{{ ECHO_RAW_STRING }} {!! ECHO_VARIABLE_WITHOUT_ESCAPING_STRING !!} Control @if(), @elseif(), @else, @endif @for(), @endfor @foreach(), @endforeach Explore Example run php artisan make:auth and check your views directory
  • 8.
    Laravel: Model ● Modelis where we control Database Logic ● Model files are store in app/*.php ● Model file names is Database Table Name ● We rarely use SQL in Laravel, the data record to becomes object ● We should already have app/User.php from the php artisan make:auth ● To make new model, we run php artisan make:model ModelName
  • 9.
    Laravel: Migration ● Migrationis a version control for database schema for Laravel ● This helps you create database schema via PHP Syntax ● Go check your database/migrations/*.php files ● There are function up() and down() in each migration file ● up() indicates what to do in forward ● down() indicates what to do in backward ● We have a lot of migration commands ● Setup your database configuration in .env file ● Run php artisan migrate ● Check your database schema migrate migrate:install Create the migration repository migrate:refresh Reset and re-run all migrations migrate:reset Rollback all database migrations migrate:rollback Rollback the last database migration migrate:status Show the status of each migration
  • 10.
    Laravel: Eloquent ● Eloquentis an ORM (Object-relational mapping) helps you manage database query via OOP ● 1-to-1, 1-to-many, many-to-many are also here! ● This allows us to do e.g. user->comments()->get(); to get all comments for a certain user ● Make a new record? $newRecord = new ModelName([“FIELD”=>”DATA”]); $newRecord->save(); ● Too many things to discuss here, have a look at https://laravel.com/docs/5.3/eloquent
  • 11.
    Laravel: Controller ● Controlleris your main logic for a certain request ● Laravel help controller to be super small compare to other frameworks ● The controller files are in app/Http/Controllers/**/*.php ● Make a new controller? php artisan make:controller NameController Let’s Explore Controllers ● Open app/Http/Controllers/HomeController.php ● Open routes/web.php ● Route::get('/home', 'HomeController@index'); tells laravel to run HomeController method index in /home
  • 12.
    Laravel: Resource Controller ●As I said, Laravel is designed for productivity, it assumes that controller would done CRUD tasks, so it has resource controller for this. ● The default functions: index, create, store, show, edit, update, destroy ● Make a new resource controller? php artisan make:controller --resource NameController ● Map route to resource controller? Route::resource('/name, 'NameController'); ● Run php artisan route:list
  • 13.
    Workshop & Assignment Developa web blog with comments with mocked user data *Hint - Getting a request from HTML form using $request from post route