├── .gitignore ├── README.md ├── SUMMARY.md ├── checklist.md ├── section-1.md ├── section-2.md ├── section-3.md ├── section-4.md ├── section-5.md └── section-6.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Node rules: 2 | ## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 3 | .grunt 4 | 5 | ## Dependency directory 6 | ## Commenting this out is preferred by some people, see 7 | ## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git 8 | node_modules 9 | 10 | # Book build output 11 | _book 12 | 13 | # eBook build output 14 | *.epub 15 | *.mobi 16 | *.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Cookbook 2 | 3 | Laravel Cookbook based on author experiences in teaching Laravel Framework. This book intended to simplify the understanding on how to develop web applications with Laravel Framework. 4 | 5 | ## [Section 1: Data Preparation](https://github.com/nasrulhazim/laravel-cookbook/blob/master/section-1.md) 6 | 7 | In this section, developers will be exposed, how to prepare your data from scratch. Following are the basic steps in preparing data. 8 | 9 | 1. Create model & migration script 10 | 2. Setup Migration Script Schema 11 | 3. Setup Fillable 12 | 4. Run Migration Scripts 13 | 5. Setup Factory 14 | 6. Setup Seeder 15 | 7. Seed the Data 16 | 8. Seed the Data via Tinker 17 | 18 | ## [Section 2: How to create a page?](https://github.com/nasrulhazim/laravel-cookbook/blob/master/section-2.md) 19 | 20 | In web applications development, creating pages, we have a standard operation procedure \(SOP\). Following are the SOP and user cases in creating pages across web applications. 21 | 22 | 1. Route, View\(closure\) 23 | 2. Route, Controller & View \(string\) 24 | 3. Route, Controller & View \(blade template\) 25 | 4. Route, Controller, View \(Blade\), Data \(Eloquent\) 26 | 27 | ## [Section 3: Form and Validation](https://github.com/nasrulhazim/laravel-cookbook/blob/master/section-3.md) 28 | 29 | Forms and Validations are most common components in web applications development. In this section, developer should grasp the fundamental of creating forms and validations. 30 | 31 | 1. Form - Create, Update & Delete 32 | 2. Validation 33 | 1. Controller 34 | 2. Custom Request 35 | 36 | ## [Section 4: Relationship](https://github.com/nasrulhazim/laravel-cookbook/blob/master/section-4.md) 37 | 38 | Database design is the most critical and fundamental in web applications development. Managing relationship between tables is critical. In this section, developers should be able to grasp the SOP fo creating relationships between tables and how to query the data. Following are the basic relationships covered in this section. 39 | 40 | 1. One-to-one 41 | 2. One-to-many 42 | 3. Many-to-one 43 | 4. Many-to-many 44 | 45 | ## [Section 5: Common Request](https://github.com/nasrulhazim/laravel-cookbook/blob/master/section-5.md) 46 | 47 | Following are the common request in developing web applications. 48 | 49 | 1. Setup Multilingual 50 | 2. Setup Custom Validation Messages 51 | 52 | ## [Section 6: Intermediate Request](https://github.com/nasrulhazim/laravel-cookbook/blob/master/section-6.md) 53 | 54 | This section covered request from intermediate level developers. 55 | 56 | 1. Middleware 57 | 58 | ## [Checklist](https://github.com/nasrulhazim/laravel-cookbook/blob/master/checklist.md) 59 | 60 | In this section, the author have come up questions checklist as a test, to check either developers know and understand how to develop web applications with Laravel Framework. 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | * [Introduction](README.md) 4 | * [Section 1: Data Preparation](section-1.md) 5 | * [Section 2: How to create a page?](section-2.md) 6 | * [Section 3: Form and Validation](section-3.md) 7 | * [Section 4: Relationship](section-4.md) 8 | * [Section 5: Common Request](section-5.md) 9 | * [Section 6: Intermediate Request](section-6.md) 10 | * [Checklist](checklist.md) 11 | 12 | -------------------------------------------------------------------------------- /checklist.md: -------------------------------------------------------------------------------- 1 | # Checklist 2 | 3 | * [ ] How to create a new Laravel project? 4 | * [ ] How to create a page? 5 | * [ ] How to create a route? 6 | * [ ] How to create a controller? 7 | * [ ] How to create a view? 8 | * [ ] How to create users from tinker? 9 | * [ ] How to read a record? 10 | * [ ] How to read records? 11 | * [ ] How to create pagination? 12 | * [ ] How to create a form? 13 | * [ ] How to validate form using Request? 14 | * [ ] How to validate form in Controller? 15 | * [ ] How to insert a new record? 16 | * [ ] How to update a record? 17 | * [ ] How to create method spoofing? 18 | * [ ] How to get error messages from validators? 19 | * [ ] How to create a model and migration script together? 20 | * [ ] How to create a model factory? 21 | * [ ] How to create seeder file? 22 | * [ ] How to call model factory in seeder file? 23 | * [ ] How to call seeder file in `DatabaseSeeder.php`? 24 | * [ ] How to seed data? 25 | * [ ] How to create relationship? 26 | * [ ] How to setup custom validation error messages? 27 | * [ ] How to setup localization? 28 | * [ ] How to setup custom validation error messages with localization support? 29 | * [ ] How to register middleware? 30 | * [ ] How to use middleware? 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /section-1.md: -------------------------------------------------------------------------------- 1 | # Section 1: Data Preparation 2 | 3 | ## Create Model & Migration Script 4 | 5 | Run following command in terminal: 6 | 7 | ``` 8 | php artisan make:model Post -m 9 | ``` 10 | 11 | This will create a model named `Post.php` in `app` folder and a migration script `timestamp_create_posts_table.php` in `database\migrations` folder. 12 | 13 | ## Setup Migration Script Schema 14 | 15 | Open up your `timestamp_create_posts_table.php` in `database\migrations` folder and update the schema accordingly. 16 | 17 | ```php 18 | Schema::create('posts', function (Blueprint $table) { 19 | $table->increments('id'); 20 | $table->string('post'); 21 | $table->timestamps(); 22 | }); 23 | ``` 24 | 25 | Read more about [Creating Columns](https://laravel.com/docs/5.3/migrations#creating-columns) 26 | 27 | ## Setup Fillable 28 | 29 | In order to user `Post::create()` method, you need to define the `$fillable` property of the model class. 30 | 31 | ```php 32 | protected $fillable = [ 33 | 'post', 34 | ]; 35 | ``` 36 | 37 | ## Run Migration Scripts 38 | 39 | Run the following command to check the migration status: 40 | 41 | ``` 42 | php artisan migrate:status 43 | ``` 44 | 45 | If there's still `N` status, you can run the following command to run the migration scripts. 46 | 47 | ``` 48 | php artisan migrate 49 | ``` 50 | 51 | ## Setup Factory 52 | 53 | Where you define your data format, how it should be inserted when calling it from `factory()` helper. 54 | 55 | ```php 56 | $factory->define(App\Post::class, function (Faker\Generator $faker) { 57 | return [ 58 | 'post' => $faker->sentence, 59 | ]; 60 | }); 61 | ``` 62 | 63 | ## Setup Seeder 64 | 65 | Create a seeder file using following command: 66 | 67 | ```php 68 | php artisan make:seeder PostTableSeeder 69 | ``` 70 | 71 | Open `PostTableSeeder.php` located at `database\seeds` folder and call the factory for `Post`, as following in `run` method: 72 | 73 | ```php 74 | factory(\App\Post::class, 100)->create(); 75 | ``` 76 | 77 | ### How to Seed Data 78 | 79 | There's three ways in seeding data. 80 | 81 | #### Method 1 82 | 83 | Following command will seed data by calling `DatabaseSeeder.php`: 84 | 85 | ``` 86 | php artisan db:seed 87 | ``` 88 | 89 | #### Method 2 90 | 91 | Following command will seed data by calling seeder class name: 92 | 93 | ``` 94 | php artisan db:seed --class=PostSeeder 95 | ``` 96 | 97 | #### Method 3 98 | 99 | Following command will seed data after do the migration: 100 | 101 | ``` 102 | php artisan migrate --seed 103 | ``` 104 | 105 | -------------------------------------------------------------------------------- /section-2.md: -------------------------------------------------------------------------------- 1 | # Section 2: How to create a page 2 | 3 | ## Route 4 | 5 | Set Route - `routes/web.php` 6 | 7 | ```php 8 | Route::get('about-us','PageController@aboutUs') 9 | ``` 10 | 11 | ## Controller 12 | 13 | Create a controller - `php artisan make:controller PageController --resource` - and create a method `aboutUs` returning view from `pages.index`. 14 | 15 | ```php 16 | public function aboutUs() 17 | { 18 | return view('pages.index'); 19 | } 20 | ``` 21 | 22 | ### Pass Data to Blade Template 23 | 24 | There's 3 methods available on how to pass data from controller to blade template. 25 | 26 | #### Method 1 27 | 28 | ```php 29 | public function aboutUs() 30 | { 31 | $company = 'Cleanique Coders Resources'; 32 | $founder = 'Nasrul Hazim'; 33 | 34 | return view('pages.index') 35 | ->with('company', $company) 36 | ->with('founder', $founder); 37 | } 38 | ``` 39 | 40 | #### Method 2 41 | 42 | ```php 43 | public function aboutUs() 44 | { 45 | $company = 'Cleanique Coders Resources'; 46 | $founder = 'Nasrul Hazim'; 47 | 48 | return view('pages.index',[ 49 | 'company' => $company, 50 | 'founder' => $founder, 51 | ]); 52 | } 53 | ``` 54 | 55 | 56 | #### Method 3 57 | 58 | ```php 59 | public function aboutUs() 60 | { 61 | $company = 'Cleanique Coders Resources'; 62 | $founder = 'Nasrul Hazim'; 63 | 64 | return view('pages.index',compact('company','founder')); 65 | } 66 | ``` 67 | 68 | ## View 69 | 70 | Create the view - create new folder in `resources/views/` named `pages`. In `pages`, create `aboutUs.blade.php`. Add the following content: 71 | 72 | ```html 73 | @extends('layouts.app') 74 | 75 | @section('content') 76 |