├── 9781484249901.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md ├── chapter-eight ├── chapter-eleven ├── chapter-five ├── chapter-four ├── chapter-nine ├── chapter-one ├── chapter-seven ├── chapter-six ├── chapter-ten ├── chapter-three ├── chapter-twelve ├── chapter-two └── errata.md /9781484249901.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beginning-laravel-2e/f1816bcecc736a36daf7bd095e74a041adf2ba89/9781484249901.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2019 Sanjib Sinha 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Beginning Laravel, 2nd Edition*](https://www.apress.com/9781484249901) by Sanjib Sinha (Apress, 2019). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484249901.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /chapter-eight: -------------------------------------------------------------------------------- 1 | //code 8.1 2 | $ php artisan make:middleware CheckRole 3 | Middleware created successfully. 4 | 5 | //code 8.2 6 | // app/Http/Middleware/CheckRole.php 7 | check() && $request->user()->admin == 0){ 32 | return redirect()->guest('home'); 33 | } 34 | return $next($request); 35 | } 36 | } 37 | 38 | 39 | 40 | //code 8.4 41 | //‘routes/web.php’ 42 | Route::group(['middleware' => ['web', 'auth']], function(){ 43 | Route::get('/adminonly', function () { 44 | if(Auth::user()->admin == 0){ 45 | return view('restrict'); 46 | }else{ 47 | $users['users'] = \App\User::all(); 48 | return view('adminonly', $users); 49 | } 50 | }); 51 | }); 52 | 53 | 54 | //code 8.5 55 | // resources/views/adminonly.blade.php 56 | @extends('layouts.app') 57 | @section('content') 58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | @if (session('status')) 66 |
67 | {{ session('status') }} 68 |
69 | @endif 70 |

THIS IS ADMIN PAGE

71 |

ADMIN CAN ALSO DO

72 |

SOMETHING ELSE HERE

73 | HOME 74 |

PLEASE VISIT ABOVE HOME LINK FOR FURTHER EDITING

75 |
83 | @endsection 84 | 85 | 86 | 87 | //code 8.6 88 | //‘app/Http/Kernel.php’ 89 | protected $middleware = [ 90 | \App\Http\Middleware\CheckForMaintenanceMode::class, 91 | \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, 92 | \App\Http\Middleware\TrimStrings::class, 93 | \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 94 | \App\Http\Middleware\TrustProxies::class, 95 | \App\Http\Middleware\CheckRole::class 96 | ]; 97 | 98 | 99 | //code 8.7 100 | //routes/web.php 101 | 102 | Route::get('/', function () { 103 | return view('welcome'); 104 | }); 105 | /* 106 | Route::get('/test', function () { 107 | // 108 | return view('test'); 109 | })->middleware(CheckRole::class); 110 | */ 111 | Route::group(['middleware' => ['web', 'auth']], function(){ 112 | Route::get('/adminonly', function () { 113 | if(Auth::user()->admin == 0){ 114 | return view('restrict'); 115 | }else{ 116 | $users['users'] = \App\User::all(); 117 | return view('adminonly', $users); 118 | } 119 | }); 120 | }); 121 | 122 | Route::get('/admin', function () { 123 | if (Gate::allows('admin-only', Auth::user())) { 124 | // The current user can view this page 125 | return view('admin'); 126 | } 127 | else{ 128 | return view('restrict'); 129 | } 130 | }); 131 | 132 | Route::get('/mod', function () { 133 | if (Gate::allows('mod-only', Auth::user())) { 134 | // The current user can view this page 135 | return view('mod'); 136 | } 137 | else{ 138 | return view('restrict'); 139 | } 140 | }); 141 | 142 | Auth::routes(); 143 | 144 | Route::resource('home', 'HomeController'); 145 | 146 | Route::resource('users', 'UserController'); 147 | 148 | Route::resource('companies', 'CompanyController'); 149 | 150 | Route::resource('companies', 'CompanyController'); 151 | 152 | Route::resource('projects', 'ProjectController'); 153 | 154 | Route::resource('roles', 'RoleController'); 155 | Route::resource('tasks', 'TaskController'); 156 | 157 | Route::resource('comments', 'CommentController'); 158 | 159 | Route::resource('articles', 'ArticleController'); 160 | Route::get('/users/{id}/articles', 'ArticleController@articles'); 161 | Route::resource('reviews', 'ReviewController'); 162 | Route::get('/users/{id}/reviews', 'ReviewController@reviews'); 163 | 164 | Route::get('companies/destroy/{id}', ['as' => 'companies.get.destroy', 165 | 'uses' => 'CompanyController@getDestroy']); 166 | 167 | 168 | 169 | 170 | ----------------- 171 | 172 | 173 | //code 8.9 174 | //app/HTTP/Controllers/ArticleController.php 175 | orderBy('title', 'desc')->take(10)->get(); 196 | $users = User::all(); 197 | $tags = Tag::all(); 198 | return view('articles.index', compact('articles', 'users', 'tags')); 199 | } 200 | 201 | /** 202 | * Show the form for creating a new resource. 203 | * 204 | * @return \Illuminate\Http\Response 205 | */ 206 | public function create() 207 | { 208 | // 209 | } 210 | 211 | /** 212 | * Store a newly created resource in storage. 213 | * 214 | * @param \Illuminate\Http\Request $request 215 | * @return \Illuminate\Http\Response 216 | */ 217 | public function store(Request $request) 218 | { 219 | // 220 | } 221 | 222 | /** 223 | * Display the specified resource. 224 | * 225 | * @param int $id 226 | * @return \Illuminate\Http\Response 227 | */ 228 | public function show(Article $article) 229 | { 230 | $tags = Article::find($article->id)->tags; 231 | $article = Article::find($article->id); 232 | $comments = $article->comments; 233 | $user = User::find($article->user_id); 234 | $country = Country::where('id', $user->country_id)->get()->first(); 235 | 236 | return view('articles.show', compact('tags','article', 237 | 'country', 'comments', 'user')); 238 | } 239 | 240 | /** 241 | * Display the specified resource. 242 | * 243 | * @param \App\Article $article 244 | * @return \Illuminate\Http\Response 245 | */ 246 | public function articles($id) 247 | { 248 | $user = User::find($id); 249 | 250 | return view('articles.articles', compact('user')); 251 | } 252 | 253 | /** 254 | * Show the form for editing the specified resource. 255 | * 256 | * @param int $id 257 | * @return \Illuminate\Http\Response 258 | */ 259 | public function edit($id) 260 | { 261 | // 262 | } 263 | 264 | /** 265 | * Update the specified resource in storage. 266 | * 267 | * @param \Illuminate\Http\Request $request 268 | * @param int $id 269 | * @return \Illuminate\Http\Response 270 | */ 271 | public function update(Request $request, $id) 272 | { 273 | // 274 | } 275 | 276 | /** 277 | * Remove the specified resource from storage. 278 | * 279 | * @param int $id 280 | * @return \Illuminate\Http\Response 281 | */ 282 | public function destroy($id) 283 | { 284 | // 285 | } 286 | } 287 | 288 | 289 | 290 | ----------------- 291 | 292 | 293 | //code 8.10 294 | //app/Article.php 295 | belongsTo('App\User'); 309 | } 310 | 311 | public function users() { 312 | return $this->belongsToMany('App\User'); 313 | } 314 | 315 | public function tags() { 316 | return $this->belongsToMany('App\Tag'); 317 | } 318 | 319 | /** 320 | * Get all of the articles' comments. 321 | */ 322 | public function comments(){ 323 | return $this->morphMany('App\Comment', 'commentable'); 324 | } 325 | } 326 | 327 | 328 | -------------- 329 | 330 | //code 8.11 331 | //app/HTTP/Controllers/ ReviewController.php 332 | public function show(Review $review) 333 | { 334 | $tags = Review::find($review->id)->tags; 335 | $review = Review::find($review->id); 336 | $comments = $review->comments; 337 | $user = User::find($review->user_id); 338 | $company = Company::find($review->company_id); 339 | $country = Country::where('id', $user->country_id)->get()->first(); 340 | 341 | return view('reviews.show', compact('tags','review', 342 | 'country', 'comments', 'user', 'company')); 343 | } 344 | 345 | 346 | ------------------ 347 | 348 | //code 8.12 349 | //app/HTTP/Controllers/HomeController.php 350 | middleware('auth'); 366 | } 367 | 368 | /** 369 | * Show the application dashboard. 370 | * 371 | * @return \Illuminate\Http\Response 372 | */ 373 | public function index() 374 | { 375 | return view('home'); 376 | } 377 | } 378 | 379 | 380 | 381 | ----------------- 382 | 383 | //code 8.13 384 | check()) { 401 | //return redirect('/home'); 402 | // we have commented out the default redirection and change it to the new one 403 | return redirect('/'); 404 | } 405 | return $next($request); 406 | } 407 | } 408 | 409 | 410 | ------------------ 411 | 412 | //code 8.14 413 | //resources/views/home.blade.php 414 | @extends('layouts.app') 415 | 416 | @section('content') 417 |
418 |
419 |
420 |
421 |
Dashboard
422 | 423 |
424 | @if (session('status')) 425 | 428 | @endif 429 | 430 | You are logged in! 431 |
432 |
433 |
434 |
435 |
436 | @endsection 437 | 438 | 439 | //code 8.15 440 | //resources/views/home.blade.php 441 | @extends('layouts.app') 442 | 443 | @section('content') 444 |
445 |
446 |
447 |
448 |
449 |

Dashboard for Admin

450 |
451 |
452 | @if (session('status')) 453 |
454 | {{ session('status') }} 455 |
456 | @endif 457 | Hello
  • {{ $user->name }}
  • 458 | You are logged in! 459 |
    460 |
    461 |

    462 | Now you can view, add, edit or delete 463 | any company, project, and user 464 |

    465 |
    466 |
    467 |
    468 |
    469 |
    470 | 479 | 488 | 497 |
    498 |
    499 | @endsection 500 | 501 | 502 | 503 | //code 8.16 504 | //resources/views/home.blade.php 505 |
    506 | @if (session('status')) 507 |
    508 | {{ session('status') }} 509 |
    510 | @endif 511 | Hello
  • {{ $user->name }}
  • 512 | @if(Auth::user()->role_id === 1) 513 |

    Dashboard for Admin

    514 | You are logged in as an Administrator! 515 |

    516 | Now you can view, add, edit or delete 517 | any company, project, and user 518 |

    519 | 530 | 541 |
  • Name : {{ $article->user->name }}
  • 297 |
  • Email: {{ $article->user->email }}
  • 298 |
  • City: {{ $article->user->profile->city }}
  • 299 |
  • About: {{ $article->user->profile->about }}
  • 300 |
    301 | 302 | 303 | 304 | 305 | 306 | 307 | //code 5.15 308 | //app/Repositories/ Interfaces/UserRepositoryInterface.php 309 | Illuminate\Foundation\ComposerScripts::postAutoloadDump 355 | > @php artisan package:discover 356 | Discovered Package: beyondcode/laravel-dump-server 357 | Discovered Package: fideloper/proxy 358 | Discovered Package: laravel/tinker 359 | Discovered Package: nesbot/carbon 360 | Discovered Package: nunomaduro/collision 361 | Package manifest generated successfully. 362 | 363 | 364 | // code 5.18 365 | //app/HTTP/Controllers/UserController.php 366 | users = $users; 380 | } 381 | 382 | public function index(){ 383 | $users = $this->users->all(); 384 | return view('users.index', compact('users')); 385 | } 386 | } 387 | 388 | 389 | //code 5.19 390 | //resources/views/users/index.blade.php 391 | @foreach($users as $user) 392 |
  • 393 |

    394 | {{ $user->name }} 395 |

    396 |
  • 397 | @endforeach 398 | 399 | 400 | 401 | // code 5.20 402 | // app/Article.php 403 | /** 404 | * Get the tags for the article 405 | */ 406 | public function tags() { 407 | return $this->belongsToMany('App\Tag'); 408 | } 409 | 410 | 411 | // code 5.21 412 | // app/Country.php 413 | hasMany('App\User'); 428 | } 429 | 430 | public function articles(){ 431 | return $this->hasManyThrough('App\Article', 'App\User'); 432 | } 433 | } 434 | 435 | 436 | 437 | // code 5.22 438 | // database/migrations/countries table 439 | public function up() 440 | { 441 | Schema::create('countries', function (Blueprint $table) { 442 | $table->increments('id'); 443 | $table->string('name'); 444 | $table->timestamps(); 445 | }); 446 | } 447 | 448 | 449 | 450 | // database/migrations/users table 451 | public function up() 452 | { 453 | Schema::create('users', function (Blueprint $table) { 454 | $table->increments('id'); 455 | $table->integer('country_id'); 456 | $table->string('name'); 457 | $table->string('email')->unique(); 458 | $table->timestamp('email_verified_at')->nullable(); 459 | $table->string('password'); 460 | $table->rememberToken(); 461 | $table->timestamps(); 462 | }); 463 | } 464 | 465 | 466 | 467 | // database/factories/UserFactory.php 468 | $factory->define(App\User::class, function (Faker $faker) { 469 | return [ 470 | 'country_id' => $faker->randomDigit, 471 | 'name' => $faker->name, 472 | 'email' => $faker->unique()->safeEmail, 473 | 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret 474 | 'remember_token' => str_random(10), 475 | ]; 476 | }); 477 | 478 | 479 | 480 | //code 5.23 481 | //resources/views/articles/show.blade.php 482 | @extends('layouts.app') 483 | 484 | @section('content') 485 |
    486 |
    487 |
    488 |
    489 |
    490 |

    {{ $article->title }}

    by 491 |

    {{ $article->user->name }}

    492 |
    493 | 494 |
    495 |
  • {{ $article->body }}
  • 496 | Tags: 497 | @foreach($tags as $tag) 498 | {{ $tag->tag }}... 499 | @endforeach 500 |
  • Other Articles by 501 |

    502 | {{ $article->user->name }} 503 |

    504 | This user belongs to {{ $country->name }}

    505 |
  • 506 | 507 |

    508 | All articles from {{ $country->name }} 509 |

    510 | @foreach($country->articles as $article) 511 |
  • 512 | 513 | {{ $article->title }} 514 | 515 |
  • 516 | @endforeach 517 |
    518 |
    519 |
    520 |
    521 |
    522 | @endsection 523 | 524 | 525 | 526 | 527 | //code 5.24 528 | //database/migrations/comments table 529 | class CreateCommentsTable extends Migration 530 | { 531 | /** 532 | * Run the migrations. 533 | * 534 | * @return void 535 | */ 536 | public function up() 537 | { 538 | Schema::create('comments', function (Blueprint $table) { 539 | $table->increments('id'); 540 | $table->integer('user_id'); 541 | $table->text('body'); 542 | $table->integer('commentable_id'); 543 | $table->string('commentable_type'); 544 | $table->timestamps(); 545 | }); 546 | } 547 | 548 | /** 549 | * Reverse the migrations. 550 | * 551 | * @return void 552 | */ 553 | public function down() 554 | { 555 | Schema::dropIfExists('comments'); 556 | } 557 | } 558 | 559 | 560 | 561 | 562 | // code 5.25 563 | // app/Comment.php 564 | morphTo(); 577 | } 578 | 579 | public function user() { 580 | return $this->belongsTo('App\User'); 581 | } 582 | 583 | public function users() { 584 | return $this->belongsToMany('App\User'); 585 | } 586 | 587 | public function article() { 588 | return $this->belongsTo('App\Article'); 589 | } 590 | 591 | public function articles() { 592 | return $this->belongsToMany('App\Article'); 593 | } 594 | 595 | 596 | } 597 | 598 | 599 | 600 | ----------------------- 601 | 602 | // app/Article.php 603 | belongsTo('App\User'); 618 | } 619 | 620 | /** 621 | * Get the tags for the article 622 | */ 623 | 624 | public function tags() { 625 | return $this->belongsToMany('App\Tag'); 626 | } 627 | 628 | /** 629 | * Get all of the profiles' comments. 630 | */ 631 | public function comments(){ 632 | return $this->morphMany('App\Comment', 'commentable'); 633 | } 634 | } 635 | 636 | 637 | ---------------------- 638 | 639 | 640 | // app/Profile.php 641 | belongsTo('App\User'); 661 | 662 | } 663 | 664 | /** 665 | * Get all of the profiles' comments. 666 | */ 667 | public function comments(){ 668 | return $this->morphMany('App\Comment', 'commentable'); 669 | } 670 | } 671 | 672 | 673 | -------------------- 674 | 675 | // app/User.php 676 | hasOne('App\Profile'); 708 | 709 | } 710 | 711 | public function article() { 712 | return $this->hasOne('App\Article'); 713 | } 714 | 715 | public function articles() { 716 | return $this->hasMany('App\Article'); 717 | } 718 | 719 | public function role() { 720 | return $this->hasOne('App\Role'); 721 | } 722 | 723 | public function roles() { 724 | return $this->hasMany('App\Role'); 725 | } 726 | 727 | public function country() { 728 | return $this->beongsTo('App\Country'); 729 | } 730 | 731 | public function comment() { 732 | return $this->hasOne('App\Comment'); 733 | } 734 | 735 | public function comments() { 736 | return $this->hasMany('App\Comment'); 737 | } 738 | } 739 | 740 | 741 | --------------------------- 742 | 743 | //code 5.26 744 | // database/factories/UserFactory.php 745 | define(App\User::class, function (Faker $faker) { 761 | return [ 762 | 'country_id' => $faker->biasedNumberBetween($min = 1, $max = 20, $function = 'sqrt'), 763 | 'name' => $faker->name, 764 | 'email' => $faker->unique()->safeEmail, 765 | 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret 766 | 'remember_token' => str_random(10), 767 | ]; 768 | }); 769 | 770 | $factory->define(App\Article::class, function (Faker $faker) { 771 | return [ 772 | 'user_id' => App\User::all()->random()->id, 773 | 'title' => $faker->sentence, 774 | 'body' => $faker->paragraph(random_int(3, 5)) 775 | ]; 776 | }); 777 | 778 | $factory->define(App\Profile::class, function (Faker $faker) { 779 | return [ 780 | 'user_id' => App\User::all()->random()->id, 781 | 'city' => $faker->city, 782 | 'about' => $faker->paragraph(random_int(3, 5)) 783 | ]; 784 | }); 785 | 786 | $factory->define(App\Tag::class, function (Faker $faker) { 787 | return [ 788 | 'tag' => $faker->word 789 | ]; 790 | }); 791 | 792 | $factory->define(App\Role::class, function (Faker $faker) { 793 | return [ 794 | 'name' => $faker->word 795 | ]; 796 | }); 797 | 798 | $factory->define(App\Country::class, function (Faker $faker) { 799 | return [ 800 | 'name' => $faker->country 801 | ]; 802 | }); 803 | 804 | $factory->define(App\Comment::class, function (Faker $faker) { 805 | 806 | return [ 807 | 'user_id' => $faker->biasedNumberBetween($min = 1, $max = 10, $function = 'sqrt'), 808 | 'body' => $faker->paragraph(random_int(3, 5)), 809 | 'commentable_id' => $faker->randomDigit, 810 | 'commentable_type' => function(){ 811 | $input = ['App\Article', 'App\Profile']; 812 | $model = $input[mt_rand(0, count($input) - 1)]; 813 | return $model; 814 | } 815 | ]; 816 | }); 817 | 818 | 819 | 820 | ---------------- 821 | 822 | //code 5.27 823 | //database/seeds/DatabaseSeeder.php 824 | call(UsersTableSeeder::class); 838 | // $this->call(UsersTableSeeder::class); 839 | factory(App\User::class, 10)->create()->each(function($user){ 840 | $user->profile()->save(factory(App\Profile::class)->make()); 841 | }); 842 | 843 | factory(App\Tag::class, 20)->create(); 844 | 845 | factory(App\Country::class, 20)->create(); 846 | 847 | factory(App\Comment::class, 50)->create(); 848 | 849 | factory(App\Article::class, 50)->create()->each(function($article){ 850 | $ids = range(1, 50); 851 | shuffle($ids); 852 | $sliced = array_slice($ids, 1, 20); 853 | $article->tags()->attach($sliced); 854 | }); 855 | 856 | factory(App\Role::class, 3)->create()->each(function($role){ 857 | $ids = range(1, 2); 858 | shuffle($ids); 859 | $sliced = array_slice($ids, 1, 5); 860 | $role->users()->attach($sliced); 861 | }); 862 | } 863 | } 864 | 865 | 866 | 867 | ----------------------- 868 | 869 | 870 | //code 5.28 871 | mysql> USE laravelmodelrelations 872 | Reading table information for completion of table and column names 873 | You can turn off this feature to get a quicker startup with -A 874 | 875 | Database changed 876 | mysql> SHOW TABLES; 877 | +---------------------------------+ 878 | | Tables_in_laravelmodelrelations | 879 | +---------------------------------+ 880 | | article_tag | 881 | | articles | 882 | | comments | 883 | | countries | 884 | | migrations | 885 | | password_resets | 886 | | profiles | 887 | | role_user | 888 | | roles | 889 | | tags | 890 | | users | 891 | +---------------------------------+ 892 | 11 rows in set (0.00 sec) 893 | 894 | mysql> SELECT * FROM COMMENTS; 895 | 896 | 897 | 898 | ----------------------- 899 | 900 | 901 | //code 5.29 902 | //app/HTTP/Controllers/ArticleController.php 903 | orderBy('title', 'desc')->take(10)->get(); 925 | $users = User::all(); 926 | $tags = Tag::all(); 927 | return view('articles.index', compact('articles', 'users', 'tags')); 928 | } 929 | 930 | public function main() 931 | { 932 | $articles = Article::where('user_id', 1)->orderBy('title', 'desc')->take(4)->get(); 933 | $tags = Tag::all(); 934 | return view('welcome', ['articles' => $articles, 'tags' => $tags]); 935 | } 936 | /** 937 | * Display the specified resource. 938 | * 939 | * @param \App\Article $article 940 | * @return \Illuminate\Http\Response 941 | */ 942 | public function show(Article $article) 943 | { 944 | $tags = Article::find($article->id)->tags; 945 | $article = Article::find($article->id); 946 | $comments = $article->comments; 947 | $user = User::find($article->user_id); 948 | $country = Country::where('id', $user->country_id)->get()->first(); 949 | 950 | return view('articles.show', compact('tags','article', 951 | 'country', 'comments', 'user')); 952 | } 953 | /** 954 | * Display the specified resource. 955 | * 956 | * @param \App\Article $article 957 | * @return \Illuminate\Http\Response 958 | */ 959 | public function articles($id) 960 | { 961 | $user = User::find($id); 962 | 963 | return view('articles.articles', compact('user')); 964 | } 965 | } 966 | 967 | 968 | ----------------------- 969 | 970 | 971 | // code 5.30 972 | // resources/views/articles/show.blade.php 973 | @extends('layouts.app') 974 | 975 | @section('content') 976 |
    977 |
    978 |
    979 |
    980 |
    981 |

    {{ $article->title }}

    by 982 |

    {{ $article->user->name }}

    983 |
    984 | 985 |
    986 |
  • {{ $article->body }}
  • 987 | Tags: 988 | @foreach($tags as $tag) 989 | {{ $tag->tag }}... 990 | @endforeach 991 |
    992 |

    993 |
    994 |

    995 | All Comments for this Article 996 |

    997 | @foreach($user->profile->comments as $comment) 998 |
  • {{ $comment->body }}
  • 999 |
  • by 1000 | {{ $comment->user->name }} 1001 |
  • 1002 | @endforeach 1003 |
    1004 |
    1005 |
    1006 | 1034 |
    1035 |
    1036 | @endsection 1037 | 1038 | 1039 | ----------------------- 1040 | 1041 | 1042 | // code 5.31 1043 | // app/HTTP/Controllers/UserController.php 1044 | users = $users; 1062 | 1063 | } 1064 | /** 1065 | * Display a listing of the resource. 1066 | * 1067 | * @return \Illuminate\Http\Response 1068 | */ 1069 | public function index() 1070 | { 1071 | $users = $this->users->all(); 1072 | return view('users.index', compact('users')); 1073 | } 1074 | 1075 | /** 1076 | * Show the form for creating a new resource. 1077 | * 1078 | * @return \Illuminate\Http\Response 1079 | */ 1080 | public function create() 1081 | { 1082 | // 1083 | } 1084 | 1085 | /** 1086 | * Store a newly created resource in storage. 1087 | * 1088 | * @param \Illuminate\Http\Request $request 1089 | * @return \Illuminate\Http\Response 1090 | */ 1091 | public function store(Request $request) 1092 | { 1093 | // 1094 | } 1095 | 1096 | /** 1097 | * Display the specified resource. 1098 | * 1099 | * @param \App\User $user 1100 | * @return \Illuminate\Http\Response 1101 | */ 1102 | public function show(User $user) 1103 | { 1104 | //$roles = User::find($user->id)->roles; 1105 | $user = User::find($user->id); 1106 | return view('users.show', compact('user')); 1107 | } 1108 | 1109 | /** 1110 | * Show the form for editing the specified resource. 1111 | * 1112 | * @param \App\User $user 1113 | * @return \Illuminate\Http\Response 1114 | */ 1115 | public function edit(User $user) 1116 | { 1117 | // 1118 | } 1119 | 1120 | /** 1121 | * Update the specified resource in storage. 1122 | * 1123 | * @param \Illuminate\Http\Request $request 1124 | * @param \App\User $user 1125 | * @return \Illuminate\Http\Response 1126 | */ 1127 | public function update(Request $request, User $user) 1128 | { 1129 | // 1130 | } 1131 | 1132 | /** 1133 | * Remove the specified resource from storage. 1134 | * 1135 | * @param \App\User $user 1136 | * @return \Illuminate\Http\Response 1137 | */ 1138 | public function destroy(User $user) 1139 | { 1140 | // 1141 | } 1142 | } 1143 | 1144 | 1145 | ---------------------- 1146 | 1147 | // code 5.32 1148 | // resources/views/users/show.blade.php 1149 | @extends('layouts.app') 1150 | 1151 | @section('content') 1152 |
    1153 |
    1154 |
    1155 |
    1156 |
    1157 |

    1158 | Profile of {{ $user->name }} 1159 |

    1160 |
    1161 |
    1162 | Name : 1163 |
  • {{ $user->name }}
  • 1164 | Email : 1165 |
  • {{ $user->email }}
  • 1166 | City : 1167 |
  • {{ $user->profile->city }}
  • 1168 | About : 1169 |
  • {{ $user->profile->about }}
  • 1170 |
    1171 |
    1172 |
    1173 |
    1174 |

    1175 | All Comments about {{$user->name}} 1176 |

    1177 |
    1178 | @foreach($user->profile->comments as $comment) 1179 |
  • {{ $comment->body }}
  • 1180 |
  • by 1181 | {{ $comment->user->name }} 1182 |
  • 1183 | @endforeach 1184 |
    1185 |
    1186 |
    1187 |
    1188 |
    1189 |
    1190 |
    1191 |
    1192 | @endsection 1193 | 1194 | 1195 | -------------------- 1196 | 1197 | 1198 | -------------------------------------------------------------------------------- /chapter-four: -------------------------------------------------------------------------------- 1 | //code 4.1 2 | //app/HTTP/Controllers/TaskController.php 3 | public function show($id) 4 | { 5 | $task = Task::findOrFail($id); 6 | return view('tasks.show', compact('task')); 7 | } 8 | 9 | 10 | --------- 11 | 12 | 13 | //code 4.2 14 | //resources/views/tasks/edit.blade.php 15 |
    16 | {!! Form::model($task, ['route' => ['tasks.update', $task->id], 'method' => 'PUT']) !!} 17 | 18 |
    19 | {!! Form::label('title', 'Title', ['class' => 'awesome']) !!} 20 | {!! Form::text('title', $task->title, ['class' => 'form-control']) !!} 21 |
    22 |
    23 | {!! Form::label('body', 'Body', ['class' => 'awesome']) !!} 24 | {!! Form::textarea('body', $task->body, ['class' => 'form-control']) !!} 25 |
    26 |
    27 | {!! Form::submit('Edit Task', ['class' => 'btn btn-primary form-control']) !!} 28 |
    29 | {!! Form::close() !!} 30 |
    31 | 32 | 33 | --------------- 34 | 35 | //code 4.3 36 | //app/HTTP/Controllers/TaskController.php 37 | public function edit($id) 38 | { 39 | if(Auth::user()->is_admin == 1){ 40 | $task = Task::findOrFail($id); 41 | return view('tasks.edit', compact('task')); 42 | } 43 | else { 44 | return redirect('home'); 45 | } 46 | } 47 | 48 | 49 | --------------- 50 | 51 | //code 4.4 52 | //app/HTTP/Controllers/TaskController.php 53 | public function edit(Task $task) 54 | { 55 | if(Auth::user()->is_admin == 1){ 56 | return view('tasks.edit', compact('task')); 57 | } 58 | else { 59 | return redirect('home'); 60 | } 61 | } 62 | 63 | 64 | --------------- 65 | 66 | 67 | //code 4.5 68 | /** 69 | * Get the route key for the model. 70 | * 71 | * @return string 72 | */ 73 | public function getRouteKeyName() 74 | { 75 | return 'slug'; 76 | } 77 | 78 | 79 | ---------------- 80 | 81 | // code 4.6 82 | // database/migrations/create_password_resets_table.php 83 | public function up() 84 | { 85 | Schema::create('password_resets', function (Blueprint $table) { 86 | $table->string('email')->index(); 87 | $table->string('token'); 88 | $table->timestamp('created_at')->nullable(); 89 | }); 90 | } 91 | public function down() 92 | { 93 | Schema::dropIfExists('password_resets'); 94 | } 95 | 96 | 97 | -------------- 98 | 99 | // code 4.7 100 | $ php artisan make:model Profile -m 101 | Model created successfully. 102 | Created Migration: 2018_09_16_235301_create_profiles_table 103 | 104 | 105 | ------------- 106 | 107 | // code 4.8 108 | //profiles table comes up by default 109 | 110 | public function up() 111 | { 112 | Schema::create('profiles', function (Blueprint $table) { 113 | $table->increments('id'); 114 | $table->timestamps(); 115 | }); 116 | } 117 | 118 | 119 | ------------ 120 | 121 | // code 4.9 122 | public function up() 123 | { 124 | Schema::create('profiles', function (Blueprint $table) { 125 | $table->increments('id'); 126 | $table->integer('user_id'); 127 | $table->string('city', 64); 128 | $table->text('about'); 129 | $table->timestamps(); 130 | }); 131 | } 132 | 133 | 134 | -------------- 135 | 136 | // code 4.10 137 | $ php artisan make:model Profile -m 138 | Model created successfully. 139 | Created Migration: 2018_09_17_233619_create_profiles_table 140 | $ php artisan make:model Tag -m 141 | Model created successfully. 142 | Created Migration: 2018_09_18_013602_create_tags_table 143 | $ php artisan make:model Article -m 144 | Model created successfully. 145 | Created Migration: 2018_09_18_013613_create_articles_table 146 | $ php artisan make:model Comment -m 147 | Model created successfully. 148 | Created Migration: 2018_09_18_013629_create_comments_table 149 | 150 | $ php artisan make:migration create_article_tag_table --create=article_tag 151 | Created Migration: 2018_09_17_094743_create_article_tag_table 152 | 153 | 154 | ---------------- 155 | 156 | // code 4.11 157 | // app/User.php 158 | public function profile() { 159 | 160 | return $this->hasOne('App\Profile'); 161 | 162 | } 163 | 164 | 165 | ------------ 166 | 167 | 168 | // code 4.12 169 | // app/Profile.php 170 | public function user() { 171 | 172 | return $this->belongsTo('App\User'); 173 | } 174 | 175 | 176 | 177 | 178 | -------------- 179 | 180 | // code 4.13 181 | // tags table 182 | public function up() 183 | { 184 | Schema::create('tags', function (Blueprint $table) { 185 | $table->increments('id'); 186 | $table->string('tag'); 187 | $table->timestamps(); 188 | }); 189 | } 190 | 191 | 192 | ------------- 193 | 194 | //Tag.php 195 | belongsToMany('App\Articles'); 211 | 212 | } 213 | } 214 | 215 | 216 | ---------------- 217 | 218 | // code 4.14 219 | // articles table 220 | public function up() 221 | { 222 | Schema::create('articles', function (Blueprint $table) { 223 | $table->increments('id'); 224 | $table->integer('user_id'); 225 | $table->string('title'); 226 | $table->text('body'); 227 | $table->timestamps(); 228 | }); 229 | } 230 | 231 | 232 | --------------- 233 | 234 | //Article.php 235 | belongsTo('App\User'); 250 | } 251 | 252 | /** 253 | * Get the tags for the article 254 | */ 255 | 256 | public function tags() { 257 | return $this->belongsToMany('App\Tag'); 258 | } 259 | 260 | /** 261 | * Get all of the profiles' comments. 262 | */ 263 | public function comments(){ 264 | return $this->morphMany('App\Comment', 'commentable'); 265 | } 266 | } 267 | 268 | 269 | --------------- 270 | 271 | // code 4.15 272 | // article_tag table 273 | public function up() 274 | { 275 | Schema::create('article_tag', function (Blueprint $table) { 276 | $table->increments('id'); 277 | $table->integer('article_id'); 278 | $table->integer('tag_id'); 279 | $table->timestamps(); 280 | }); 281 | } 282 | 283 | 284 | --------------- 285 | 286 | // app/Role.php 287 | belongsTo('App\User'); 297 | } 298 | public function users() { 299 | return $this->belongsToMany('App\User'); 300 | } 301 | } 302 | 303 | ------------------ 304 | 305 | //app.User.php 306 | public function role() { 307 | return $this->belongsTo('App\Role'); 308 | } 309 | public function roles() { 310 | return $this->belongsToMany('App\Role'); 311 | } 312 | 313 | 314 | ---------------- 315 | 316 | // database/migrations/roles table 317 | public function up() 318 | { 319 | Schema::create('roles', function (Blueprint $table) { 320 | $table->increments('id'); 321 | $table->string('name'); 322 | $table->timestamps(); 323 | }); 324 | } 325 | 326 | 327 | ------------- 328 | 329 | // database/migrations/roles table 330 | public function up() 331 | { 332 | Schema::create('role_user', function (Blueprint $table) { 333 | $table->increments('id'); 334 | $table->integer('role_id'); 335 | $table->integer('user_id'); 336 | $table->timestamps(); 337 | }); 338 | } 339 | 340 | 341 | -------------- 342 | 343 | // code 4.16 344 | $ php artisan migrate 345 | Migrating: 2018_09_17_233619_create_profiles_table 346 | Migrated: 2018_09_17_233619_create_profiles_table 347 | Migrating: 2018_09_18_013602_create_tags_table 348 | Migrated: 2018_09_18_013602_create_tags_table 349 | Migrating: 2018_09_18_013613_create_articles_table 350 | Migrated: 2018_09_18_013613_create_articles_table 351 | Migrating: 2018_09_18_013629_create_comments_table 352 | Migrated: 2018_09_18_013629_create_comments_table 353 | Migrating: 2018_09_18_013956_create_article_tag_table 354 | Migrated: 2018_09_18_013956_create_article_tag_table 355 | … 356 | the list is incomplete 357 | 358 | ----------------- 359 | 360 | // code 4.17 361 | // database/factory/UserFactory.php 362 | $factory->define(App\User::class, function (Faker $faker) { 363 | return [ 364 | 'name' => $faker->name, 365 | 'email' => $faker->unique()->safeEmail, 366 | 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret 367 | 'remember_token' => str_random(10), 368 | ]; 369 | }); 370 | 371 | $factory->define(App\Article::class, function (Faker $faker) { 372 | return [ 373 | 'user_id' => App\User::all()->random()->id, 374 | 'title' => $faker->sentence, 375 | 'body' => $faker->paragraph(random_int(3, 5)) 376 | ]; 377 | }); 378 | 379 | $factory->define(App\Profile::class, function (Faker $faker) { 380 | return [ 381 | 'user_id' => App\User::all()->random()->id, 382 | 'city' => $faker->city, 383 | 'about' => $faker->paragraph(random_int(3, 5)) 384 | ]; 385 | }); 386 | 387 | $factory->define(App\Tag::class, function (Faker $faker) { 388 | return [ 389 | 'tag' => $faker->word 390 | ]; 391 | }); 392 | $factory->define(App\Role::class, function (Faker $faker) { 393 | return [ 394 | 'name' => $faker->word 395 | ]; 396 | }); 397 | 398 | 399 | ------------------- 400 | 401 | // code 4.18 402 | // database/seeds/DatabaseSeeder.php 403 | public function run() 404 | { 405 | factory(App\User::class, 10)->create()->each(function($user){ 406 | $user->profile()->save(factory(App\Profile::class)->make()); 407 | }); 408 | 409 | factory(App\Tag::class, 20)->create(); 410 | 411 | factory(App\Article::class, 50)->create()->each(function($article){ 412 | $ids = range(1, 50); 413 | shuffle($ids); 414 | $sliced = array_slice($ids, 1, 20); 415 | $article->tags()->attach($sliced); 416 | }); 417 | 418 | factory(App\Role::class, 3)->create()->each(function($role){ 419 | $ids = range(1, 5); 420 | shuffle($ids); 421 | $sliced = array_slice($ids, 1, 20); 422 | $role->users()->attach($sliced); 423 | }); 424 | 425 | } 426 | 427 | 428 | ------------------ 429 | 430 | // code 4.19 431 | $ php artisan migrate:refresh –seed 432 | 433 | 434 | ---------------- 435 | 436 | // code 4.20 437 | mysql> use laravelmodelrelations 438 | Reading table information for completion of table and column names 439 | You can turn off this feature to get a quicker startup with -A 440 | 441 | Database changed 442 | mysql> show tables; 443 | +---------------------------------+ 444 | | Tables_in_laravelmodelrelations | 445 | +---------------------------------+ 446 | | article_tag | 447 | | articles | 448 | | comments | 449 | | migrations | 450 | | password_resets | 451 | | profiles | 452 | | role_user | 453 | | roles | 454 | | tags | 455 | | users | 456 | +---------------------------------+ 457 | 10 rows in set (0.00 sec) 458 | 459 | mysql> select * from users; 460 | 461 | -------------------- 462 | 463 | // code 4.21 464 | // routes/web.php 465 | use App\Article; 466 | use App\Tag; 467 | Route::get('/', function () { 468 | $articles = Article::all(); 469 | $tags = Tag::all(); 470 | return view('welcome', ['articles' => $articles, 'tags' => $tags]); 471 | }); 472 | 473 | 474 | ---------------- 475 | 476 | // code 4.22 477 | // resources/views/ welcome.blade.php 478 | @extends('layouts.app') 479 | @section('content') 480 |
    481 |
    482 |
    483 |
    484 | 494 |
    495 | 499 | 500 |
    501 | 510 |
    511 |
    512 | @endsection 513 | 514 | 515 | ----------------------- 516 | 517 | // code 4.23 518 | // app/Article.php 519 | belongsTo('App\User'); 533 | } 534 | 535 | public function users() { 536 | return $this->belongsToMany('App\User'); 537 | } 538 | 539 | public function tags() { 540 | return $this->belongsToMany('App\Tag'); 541 | } 542 | } 543 | 544 | 545 | ------------------ 546 | 547 | // code 4.24 548 | // app/Profile.php 549 | belongsTo('App\User'); 568 | } 569 | } 570 | 571 | 572 | ----------------- 573 | 574 | // code 4.25 575 | // app/Tag.php 576 | belongsToMany('App\Articles'); 592 | 593 | } 594 | } 595 | 596 | ----------------- 597 | 598 | // code 4.26 599 | // app/User.php 600 | hasOne('App\Profile'); 631 | } 632 | 633 | public function article() { 634 | return $this->hasOne('App\Article'); 635 | } 636 | 637 | public function articles() { 638 | return $this->hasMany('App\Article'); 639 | } 640 | } 641 | 642 | 643 | --------------- 644 | 645 | // code 4.27 646 | $ php artisan make:controller CommentController --resource --model=Comment 647 | 648 | -------------- 649 | 650 | // code 4.28 651 | // app/HTTP/Controllers/CommentController.php 652 | name('home'); 707 | 708 | Route::resource('users', 'UserController'); 709 | Route::resource('profiles', 'ProfileController'); 710 | Route::resource('articles', 'ArticleController'); 711 | Route::resource('comments', 'CommentController'); 712 | Route::get('/users/{id}/articles', 'ArticleController@articles'); 713 | Route::get('/', 'ArticleController@main'); 714 | 715 | --------------------- 716 | 717 | // code 4.31 718 | // app/HTTP/Controllers/ArticleController.php 719 | public function index() 720 | { 721 | $articles = Article::orderBy('created_at','desc')->paginate(4); 722 | $users = User::all(); 723 | $tags = Tag::all(); 724 | return view('articles.index', compact('articles', 'users', 'tags')); 725 | } 726 | 727 | 728 | ----------------- 729 | 730 | // code 4.32 731 | // resources/views/articles/index.blade.php 732 | @extends('layouts.app') 733 | @section('content') 734 |
    735 |
    736 |
    737 |
    738 |
      739 | @foreach($articles as $article) 740 |
    • 741 |
    • {{ $article->title }} 742 | 743 |
    • 744 |
    • 745 | @endforeach 746 | {{ $articles->render() }} 747 |
    748 |
    749 | 752 |
    753 | 761 | 769 |
    770 |
    771 | @endsection 772 | 773 | ------------------- 774 | 775 | // code 4.33 776 | // app/HTTP/Controllers/ ArticleController.php 777 | public function main() 778 | { 779 | $articles = Article::where('user_id', 1)->orderBy('title', 'desc')->take(4)->get(); 780 | $tags = Tag::all(); 781 | return view('welcome', ['articles' => $articles, 'tags' => $tags]); 782 | } 783 | 784 | 785 | ---------------- 786 | 787 | // code 4.34 788 | //resources/views/ welcome.blade.php 789 |
      790 | @foreach($articles as $article) 791 |
    • 792 |

      793 |
    • {{ $article->title }} 794 |
    • 795 |
    • 796 | @endforeach 797 |
    798 | … 799 | 812 | 813 | 814 | ------------------- 815 | 816 | // code 4.35 817 | // app/Article.php 818 | public function user() { 819 | return $this->belongsTo('App\User'); 820 | } 821 | 822 | 823 | ----------------- 824 | 825 | 826 | 827 | 828 | -------------------------------------------------------------------------------- /chapter-nine: -------------------------------------------------------------------------------- 1 | //code 9.1 2 | Route::bind('books', function ($id){ 3 | return App\Book::where('id', $id)-->first(); 4 | }); 5 | 6 | 7 | 8 | //code 9.2 9 | public function store(Request $request) 10 | { 11 | if (Auth::check()) { 12 | // The user is logged in 13 | } 14 | } 15 | 16 | 17 | 18 | 19 | //code 9.3 20 | use Illuminate\Http\Request; 21 | use App\Http\Requests; 22 | use App\Http\Controllers\Controller; 23 | use Illuminate\Auth\Guard as Auth; 24 | class BooksController extends Controller 25 | { 26 | /** 27 | * Display a listing of the resource 28 | * 29 | * @return Response 30 | */ 31 | protected $auth; 32 | public function __construct(Auth $auth) { 33 | $this-->auth = $auth; 34 | } 35 | public function store(Request $request) 36 | { 37 | $this->auth->attempt(); 38 | } 39 | } 40 | 41 | 42 | //code 9.4 43 | namespace Illuminate\Auth; 44 | use RuntimeException; 45 | use Illuminate\Support\Str; 46 | use Illuminate\Contracts\Events\Dispatcher; 47 | use Illuminate\Contracts\Auth\UserProvider; 48 | use Symfony\Component\HttpFoundation\Request; 49 | use Symfony\Component\HttpFoundation\Response; 50 | use Illuminate\Contracts\Auth\Guard as GuardContract; 51 | use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar; 52 | use Illuminate\Contracts\Auth\Authenticatable as UserContract; 53 | use Symfony\Component\HttpFoundation\Session\SessionInterface; 54 | class Guard implements GuardContract {....} 55 | //code is incomplete for brevity 56 | 57 | 58 | 59 | //code 9.6 60 | namespace Illuminate\Contracts\Auth; 61 | interface Guard 62 | { 63 | /** 64 | * Attempt to authenticate a user using the 65 | given credentials. 66 | * 67 | * @param array $credentials 68 | * @param bool $remember 69 | * @param bool $login 70 | * @return bool 71 | */ 72 | public function attempt(array $credentials = [], 73 | $remember = false, $login = true); 74 | ..... 75 | } 76 | //this code is incomplete for brevity 77 | 78 | 79 | 80 | //code 9.7 81 | Route::get('allusers', 'UserController@getAllUsers'); 82 | 83 | 84 | //code 9.8 85 | users = $users; 132 | 133 | } 134 | //method injunction 135 | public function getAllUsers(DBUserRepository $users){ 136 | return $this->users->all(); 137 | } 138 | //other methods and code continue 139 | } 140 | 141 | 142 | 143 | //code 9.11 144 | /** 145 | * Some comments about class Baz 146 | */ 147 | 148 | class Baz {}; 149 | 150 | /** 151 | * Some comments about class Bax 152 | */ 153 | class Bax 154 | { 155 | public $baz; 156 | function __construct(Baz $baz) 157 | { 158 | $this->baz = $baz; 159 | } 160 | } 161 | 162 | /** 163 | * Some comments about class Bar 164 | */ 165 | class Bar 166 | { 167 | public $bax; 168 | function __construct(Bax $bax) 169 | { 170 | $this->bax = $bax; 171 | } 172 | } 173 | Route::get('bar', function (Bar $bar) { 174 | dd($bar); 175 | }); 176 | 177 | 178 | 179 | 180 | //code 9.12 181 | Route::get('bar', function (Bar $bar) { 182 | dd($bar->bax); 183 | }); 184 | 185 | 186 | 187 | //code 9.13 188 | class Bax implements InterfaceBax {}; 189 | 190 | class Bar 191 | { 192 | public $bax; 193 | 194 | function __construct(InterfaceBax $bax) 195 | { 196 | $this->bax = $bax; 197 | } 198 | } 199 | Route::get('bar', function (Bar $bar) { 200 | dd($bar); 201 | }); 202 | 203 | 204 | 205 | //code 9.14 206 | interface InterfaceBax 207 | { 208 | // code... 209 | } 210 | 211 | class Bax implements InterfaceBax 212 | { 213 | 214 | } 215 | class Bar 216 | { 217 | public $bax; 218 | 219 | function __construct(InterfaceBax $bax) 220 | { 221 | $this->bax = $bax; 222 | } 223 | } 224 | 225 | App::bind('Bar', function(){ 226 | return new Bar(new Bax); 227 | }); 228 | 229 | Route::get('bar', function (Bar $bar) { 230 | dd($bar); 231 | }); 232 | 233 | 234 | 235 | //code 9.15 236 | interface InterfaceBax 237 | { 238 | // code... 239 | } 240 | 241 | class Bax implements InterfaceBax 242 | { 243 | 244 | } 245 | class Bar 246 | { 247 | public $bax; 248 | 249 | function __construct(InterfaceBax $bax) 250 | { 251 | $this->bax = $bax; 252 | } 253 | } 254 | 255 | App::bind('InterfaceBax', 'Bax'); 256 | 257 | Route::get('bar', function (Bar $bar) { 258 | dd($bar); 259 | }); 260 | 261 | 262 | 263 | //code 9.16 264 | interface InterfaceBax 265 | { 266 | // code... 267 | } 268 | 269 | class Bax implements InterfaceBax 270 | { 271 | 272 | } 273 | class Bar 274 | { 275 | public $bax; 276 | 277 | function __construct(InterfaceBax $bax) 278 | { 279 | $this->bax = $bax; 280 | } 281 | } 282 | 283 | App::bind('InterfaceBax', 'Bax'); 284 | 285 | Route::get('bar', function () { 286 | $bar = App::make('InterfaceBax'); 287 | dd($bar); 288 | }); 289 | 290 | 291 | 292 | //code 9.17 293 | Route::get('bar', function () { 294 | $bar = app()->make('InterfaceBax'); 295 | dd($bar); 296 | }); 297 | 298 | 299 | //code 9.18 300 | Route::get('bar', function () { 301 | $bar = app()['InterfaceBax']; 302 | dd($bar); 303 | }); 304 | 305 | 306 | 307 | -------------------------------------------------------------------------------- /chapter-one: -------------------------------------------------------------------------------- 1 | //code 1.1 2 | //Laravel Route Example 3 | Route::get('/', function () { 4 | return view('welcome'); 5 | }); 6 | 7 | 8 | ----------------------- 9 | 10 | 11 | //How to use Abstraction using higher and lower modules 12 | interface PaymentInterface { 13 | public function notify(); 14 | } 15 | 16 | 17 | class PaymentGateway implements PaymentInterface { 18 | 19 | public function notify() { 20 | 21 | return "You have paid through Stripe"; 22 | 23 | } 24 | 25 | } 26 | 27 | 28 | class PaymentNotification { 29 | 30 | protected $notify; 31 | 32 | public function __construct(PaymentInterface $notify) { 33 | 34 | $this->notify = $notify; 35 | 36 | } 37 | 38 | public function notifyClient() { 39 | 40 | return "We have recieved your payment. {$this->notify->notify()}"; 41 | 42 | } 43 | 44 | 45 | 46 | } 47 | 48 | $notify = new PaymentNotification(new PaymentGateway); 49 | 50 | echo $notify->notifyClient(); 51 | 52 | 53 | ------------------------- 54 | 55 | //code 1.16 56 | //config/app.php 57 | 'providers' => [ 58 | 59 | /* 60 | * Laravel Framework Service Providers... 61 | */ 62 | Illuminate\Auth\AuthServiceProvider::class, 63 | Illuminate\Broadcasting\BroadcastServiceProvider::class, 64 | Illuminate\Bus\BusServiceProvider::class, 65 | Illuminate\Cache\CacheServiceProvider::class, 66 | … 67 | App\Providers\EventServiceProvider::class, 68 | App\Providers\RouteServiceProvider::class, 69 | 70 | ], 71 | 72 | 73 | ------------------------ 74 | 75 | 76 | -------------------------------------------------------------------------------- /chapter-seven: -------------------------------------------------------------------------------- 1 | //code 7.1 2 | $ php artisan list 3 | 4 | 5 | //code 7.2 6 | $ php artisan help migrate 7 | 8 | 9 | 10 | 11 | //code 7.3 12 | Description: 13 | Run the database migrations 14 | 15 | Usage: 16 | migrate [options] 17 | 18 | Options: 19 | --database[=DATABASE] The database connection to use 20 | --force Force the operation to run when in production 21 | --path[=PATH] The path to the migrations files to be executed 22 | --realpath Indicate any provided migration file paths are pre-resolved absolute paths 23 | --pretend Dump the SQL queries that would be run 24 | --seed Indicates if the seed task should be re-run 25 | --step Force the migrations to be run so they can be rolled back individually 26 | -h, --help Display this help message 27 | -q, --quiet Do not output any message 28 | -V, --version Display this application version 29 | --ansi Force ANSI output 30 | --no-ansi Disable ANSI output 31 | -n, --no-interaction Do not ask any interactive question 32 | --env[=ENV] The environment the command should run under 33 | -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug 34 | 35 | 36 | 37 | 38 | 39 | //code 7.4 40 | $ php artisan migrate -V 41 | 42 | 43 | 44 | 45 | //code 7.5 46 | composer create-project --prefer-dist laravel/laravel laravelstarttofinish 47 | 48 | 49 | //code 7.6 50 | composer create-project --prefer-dist laravel/laravel laravelstarttofinish "5.8.*" 51 | 52 | 53 | 54 | 55 | //code 7.7 56 | php artisan make:auth 57 | 58 | 59 | // code 7.8 60 | php artisan migrate 61 | 62 | 63 | //code 7.9 64 | // it creates model and table simultaneously 65 | php artisan make:model Profile -m 66 | 67 | 68 | //code 7.10 69 | php artisan migrate:rollback 70 | 71 | 72 | //code 7.11 73 | php artisan make:controller ProfileController --resource 74 | 75 | 76 | //code 7.12 77 | php artisan make:controller ProfileController --resource –model=Profile 78 | 79 | 80 | // code 7.14 81 | php artisan make:migration create_article_tag_table --create=article_tag 82 | 83 | 84 | //code 7.15 85 | php artisan db:seed 86 | 87 | 88 | //code 7.16 89 | // to update seeds 90 | php artisan migrate:refresh --seed 91 | 92 | 93 | 94 | //code 7.17 95 | php artisan serve 96 | 97 | 98 | 99 | // code 7.18 100 | php artisan tinker 101 | 102 | 103 | //code 7.19 104 | $ php artisan tinker 105 | 106 | 107 | //code 7.20 108 | $list = new Listtodo; 109 | $list->name = 'First Job'; 110 | $list->description = 'Go to market'; 111 | $list->save(); 112 | 113 | 114 | //code 7.21 115 | $list = Listtodo::create( 116 | array('name' => 'First Job', 117 | 'description' => 'Go to Market') 118 | ); 119 | 120 | 121 | //code 7.22 122 | echo Listtodo::all()->count(); 123 | 124 | 125 | //code 7.23 126 | $lists = Listtodo::select('name', 'description')->first(); 127 | 128 | 129 | //7.24 130 | $lists = Listtodo::orderBy('name')->get(); 131 | 132 | 133 | //code 7.25 134 | $lists = Listtodo::orderBy('name', 'DESC')->get(); 135 | 136 | //code 7.26 137 | $lists = Listtodo::where('isComplete', '=', 1)->get(); 138 | 139 | 140 | //code 7.27 141 | $lists = Listtodo::take(5)->orderBy('created_at', 'desc')->get(); 142 | 143 | 144 | //code 7.28 145 | lists = Listtodo::take(5)->skip(5)->orderBy('created_at', 'desc')->get(); 146 | 147 | 148 | //code 7.29 149 | $list = Listtodo::all()->random(1); 150 | $list = Listtodo::orderBy(DB::raw('RAND()'))->first(); 151 | 152 | 153 | //code 7.30 154 | $list = Listtodo::find(1); 155 | $list->name = 'Going to market and buying fish'; 156 | $list->save(); 157 | 158 | 159 | //code 7.31 160 | $list = Listtodo::updateOrCreate( 161 | array('name' => 'Going to market and buying fish'), 162 | 163 | 164 | //code 7.32 165 | $list = Listtodo::find(2); 166 | $list->delete(); 167 | Listtodo::destroy(2); 168 | 169 | 170 | //code 7.33 171 | $lists = DB::table('liststodos')->get(); 172 | foreach ($lists as $list) { 173 | echo $list->name; 174 | } 175 | 176 | 177 | //code 7.34 178 | $list = DB::table('liststodos')->find(6); 179 | 180 | 181 | 182 | //code 7.35 183 | $lists = DB::table('liststodos')->select('name')->get(); 184 | 185 | 186 | 187 | //code 7.36 188 | $lists = DB::select('SELECT * from todolists'); 189 | 190 | 191 | //code 7.37 192 | DB::insert('insert into todolists (name, description) values (?, ?)', 193 | array('Second job', 'Finishing the last chapter'); 194 | 195 | 196 | //code 7.38 197 | DB::delete('delete from todolists where completed = 1'); 198 | 199 | 200 | //code 7.39 201 | $lists = DB::statement('drop table todolists'); 202 | 203 | 204 | 205 | -------------------------------------------------------------------------------- /chapter-six: -------------------------------------------------------------------------------- 1 | //code 6.1 2 | Route::post('user/profile', function () { 3 | // Validate the request... 4 | return back()->withInput(); 5 | }); 6 | 7 | 8 | //code 6.2 9 | //app/Http//Controller/ArticleController.php 10 | public function index() 11 | { 12 | // 13 | if(Auth::user()->is_admin == 1){ 14 | $articles = Article::orderBy('published_at', 'asc')->get(); 15 | return view('articles.index', compact('articles')); 16 | } 17 | else { 18 | return redirect('home'); 19 | } 20 | } 21 | 22 | 23 | 24 | 25 | 26 | //code 6.3 27 | return redirect()->route('login'); 28 | 29 | 30 | 31 | 32 | 33 | //code 6.4 34 | // For a route with the following URI: article/{id} 35 | return redirect()->route('article', ['id' => 1]); 36 | 37 | 38 | 39 | //code 6.5 40 | return redirect()->action('ArticleController@index'); 41 | 42 | 43 | 44 | //code 6.6 45 | return redirect()->action( 46 | 'ArticleController@show', ['id' => 1] 47 | ); 48 | 49 | 50 | 51 | 52 | //code 6.7 53 | public function update(Request $request, $id) 54 | { 55 | // 56 | if(Auth::user()->is_admin == 1){ 57 | 58 | if($file = $request->file('image')){ 59 | 60 | $name = $file->getClientOriginalName(); 61 | 62 | $post = Article::findOrFail($id); 63 | $post->title = $request->input('title'); 64 | $post->body = $request->input('body'); 65 | $post->published_at = $request->input('published_at'); 66 | $post->image = $name; 67 | $post->save(); 68 | 69 | $file->move('images/upload', $name); 70 | } 71 | else { 72 | // code... 73 | $post = Article::findOrFail($id); 74 | $post->title = $request->input('title'); 75 | $post->body = $request->input('body'); 76 | $post->published_at = $request->input('published_at'); 77 | $post->save(); 78 | 79 | } 80 | 81 | if($post){ 82 | return redirect('articles')->with('status', 'Article Updated!'); 83 | } 84 | } 85 | } 86 | 87 | 88 | 89 | //code 6.8 90 | //resources/views/articles/index.blade.php 91 | @if (session('status')) 92 |
    93 | {{ session('status') }} 94 |
    95 | @endif 96 | 97 | 98 | 99 | 100 | //code 6.9 101 | public function store(Request $request) 102 | { 103 | if($file = $request->file('image')){ 104 | $name = $file->getClientOriginalName(); 105 | $post = new Article; 106 | $post->title = $request->input('title'); 107 | $post->body = $request->input('body'); 108 | $post->published_at = $request->input('date'); 109 | $post->image = $name; 110 | 111 | $post->save(); 112 | 113 | $file->move('images/upload', $name); 114 | 115 | } 116 | 117 | if($post){ 118 | return redirect('articles')->with('status', 'Article Created!'); 119 | } 120 | } 121 | 122 | 123 | //code 6.10 124 | /** 125 | * Update the specified resource in storage. 126 | * 127 | * @param \Illuminate\Http\Request $request 128 | * @param int $id 129 | * @return \Illuminate\Http\Response 130 | */ 131 | public function update(Request $request, $id) 132 | { 133 | // 134 | if(Auth::user()->is_admin == 1){ 135 | 136 | if($file = $request->file('image')){ 137 | 138 | $name = $file->getClientOriginalName(); 139 | 140 | $post = Article::findOrFail($id); 141 | $post->title = $request->input('title'); 142 | $post->body = $request->input('body'); 143 | $post->published_at = $request->input('published_at'); 144 | $post->image = $name; 145 | $post->save(); 146 | 147 | $file->move('images/upload', $name); 148 | } 149 | else { 150 | // code... 151 | $post = Article::findOrFail($id); 152 | $post->title = $request->input('title'); 153 | $post->body = $request->input('body'); 154 | $post->published_at = $request->input('published_at'); 155 | $post->save(); 156 | 157 | } 158 | if($post){ 159 | return redirect('articles')->with('status', 'Article Updated!'); 160 | } 161 | } 162 | } 163 | 164 | 165 | 166 | 167 | //code 6.11 168 | Route::get('/', function () { 169 | return [1, 2, 3]; 170 | }); 171 | 172 | 173 | 174 | //code 6.12 175 | Route::get('post/create', 'ArticlePostController@create'); 176 | Route::post('post', 'ArticleController@store'); 177 | 178 | 179 | //code 6.13 180 | /** 181 | * Store a new post content. 182 | * 183 | * @param Request $request 184 | * @return Response 185 | */ 186 | public function store(Request $request) 187 | { 188 | $validatedData = $request->validate([ 189 | 'title' => 'required|unique:posts|max:255', 190 | 'body' => 'required', 191 | ]); 192 | // The content is valid... 193 | } 194 | 195 | 196 | 197 | 198 | 199 | 200 | //code 6.14 201 | validate([ 229 | 'title' => 'bail|required|unique:posts|max:255', 230 | 'body' => 'required', 231 | ]); 232 | // The content is valid... 233 | } 234 | } 235 | 236 | 237 | 238 | //code 6.15 239 | 240 | 241 |

    Create Post

    242 | 243 | @if ($errors->any()) 244 |
    245 |
      246 | @foreach ($errors->all() as $error) 247 |
    • {{ $error }}
    • 248 | @endforeach 249 |
    250 |
    251 | @endif 252 | 253 | 254 | 255 | 256 | 257 | //code 6.16 258 | $request->validate([ 259 | 'title' => 'bail|required|unique:posts|max:255', 260 | 'body' => 'required', 261 | 'publish_at' => 'nullable|date', 262 | ]); 263 | 264 | 265 | 266 | 267 | //code 6.17 268 | //app/HTTP/Controllers/ArticleController.php 269 | public function store(Request $request) 270 | { 271 | // 272 | $validatedData = $request->validate([ 273 | 'title' => 'required|unique:posts|max:255', 274 | 'body' => 'required', 275 | ]); 276 | if($file = $request->file('image')){ 277 | 278 | $name = $file->getClientOriginalName(); 279 | 280 | $post = new Article; 281 | $post->title = $request->input('title'); 282 | $post->body = $request->input('body'); 283 | $post->published_at = $request->input('date'); 284 | $post->image = $name; 285 | 286 | $post->save(); 287 | 288 | $file->move('images/upload', $name); 289 | 290 | } 291 | 292 | if($post){ 293 | return redirect('articles'); 294 | } 295 | } 296 | 297 | 298 | 299 | //code 6.18 300 | //app/HTTP/Controllers/ArticleController.php 301 | class ArticleController extends Controller 302 | { 303 | public function store(Request $request) 304 | { 305 | $validator = Validator::make($request->all(), [ 306 | 'title' => 'required|unique:posts|max:255', 307 | 'body' => 'required', 308 | ]); 309 | 310 | if ($validator->fails()) { 311 | return redirect('post/create') 312 | ->withErrors($validator) 313 | ->withInput(); 314 | } 315 | //code... 316 | } 317 | } 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | //code 6.19 326 |
    327 | @csrf 328 | Another one we can use with 'laravelcollective/html' package: 329 | //code 330 | {!! Form::open(['url' => 'foo/bar']) !!} 331 | // 332 | {!! Form::close() !!} 333 | 334 | 335 | 336 | //code 6.20 337 | composer require "laravelcollective/html":"^5.4.0" 338 | 339 | 340 | //code 6.21 341 | 'providers' => [ 342 | // ... 343 | Collective\Html\HtmlServiceProvider::class, 344 | // ... 345 | ], 346 | 347 | 348 | 349 | //code 6.22 350 | 'aliases' => [ 351 | // ... 352 | 'Form' => Collective\Html\FormFacade::class, 353 | 'Html' => Collective\Html\HtmlFacade::class, 354 | // ... 355 | ], 356 | 357 | 358 | 359 | //code 6.23 360 | //resources/views/articles/create.blade.php 361 |
    362 | 363 | {!! Form::open(['url' => 'articles', 'files' => true]) !!} 364 |
    365 | {!! Form::label('title', 'Title', ['class' => 'awesome']) !!} 366 | {!! Form::text('title', 'Give a good title', ['class' => 'form-control']) !!} 367 |
    368 |
    369 | 370 | {!! Form::label('body', 'Body', ['class' => 'awesome']) !!} 371 | {!! Form::textarea('body', 'Write your article Content', ['class' => 'form-control']) !!} 372 |
    373 |
    374 | {!! Form::label('published_at', 'Published On', ['class' => 'awesome']) !!} 375 | {!! Form::input('date', 'published_at', null, ['class' => 'form-control']) !!} 376 |
    377 |
    378 | {!! Form::file('image') !!} 379 |
    380 |
    381 | {!! Form::submit('Add Article', ['class' => 'btn btn-primary form-control']) !!} 382 |
    383 | {!! Form::close() !!} 384 |
    385 | 386 | 387 | 388 | 389 | 390 | 391 | //code 6.24 392 | {!! Form::open(array('route' => 'route.name')) !!} 393 | {!! Form::open(array('action' => 'Controller@method')) !!} 394 | 395 | 396 | 397 | 398 | //code 6.25 399 | //app/HTTP/Controllers/ArticleController.php 400 | 401 | public function create() 402 | { 403 | if(Auth::user()->is_admin == 1){ 404 | return view('articles.create'); 405 | } 406 | else { 407 | return redirect('home'); 408 | } 409 | } 410 | public function store(Request $request) 411 | { 412 | if($file = $request->file('image')){ 413 | $name = $file->getClientOriginalName(); 414 | $post = new Article; 415 | $post->title = $request->input('title'); 416 | $post->body = $request->input('body'); 417 | $post->published_at = $request->input('date'); 418 | $post->image = $name; 419 | $post->save(); 420 | $file->move('images/upload', $name); 421 | } 422 | 423 | 424 | 425 | 426 | 427 | //code 6.26 428 | //resources/views/articles/edit.blade.php 429 |
    430 | {!! Form::model($article, ['route' => ['articles.update', $article->id], 'method' => 'PUT', 'files' => true]) !!} 431 | 432 |
    433 | {!! Form::label('title', 'Title', ['class' => 'awesome']) !!} 434 | {!! Form::text('title', "$article->title", ['class' => 'form-control']) !!} 435 |
    436 |
    437 | {!! Form::label('body', 'Body', ['class' => 'awesome']) !!} 438 | {!! Form::textarea('body', "$article->body", ['class' => 'form-control']) !!} 439 |
    440 |
    441 | {!! Form::label('published_at', 'Published On', ['class' => 'awesome']) !!} 442 | {!! Form::input('date', 'published_at', null, ['class' => 'form-control']) !!} 443 |
    444 |
    445 | {!! Form::file('image') !!} 446 |
    447 |
    448 | {!! Form::submit('Edit Article', ['class' => 'btn btn-primary form-control']) !!} 449 |
    450 | {!! Form::close() !!} 451 |
    452 | 453 | 454 | 455 | //code 6.27 456 | //app/HTTP/Controllers/ArticleController.php 457 | public function edit($id) 458 | { 459 | if(Auth::user()->is_admin == 1){ 460 | $article = Article::findOrFail($id); 461 | return view('articles.edit', compact('article')); 462 | } 463 | else { 464 | return redirect('home'); 465 | } 466 | } 467 | public function update(Request $request, $id) 468 | { 469 | if(Auth::user()->is_admin == 1){ 470 | 471 | if($file = $request->file('image')){ 472 | 473 | $name = $file->getClientOriginalName(); 474 | 475 | $post = Article::findOrFail($id); 476 | $post->title = $request->input('title'); 477 | $post->body = $request->input('body'); 478 | $post->published_at = $request->input('published_at'); 479 | $post->image = $name; 480 | $post->save(); 481 | $file->move('images/upload', $name); 482 | } 483 | else { 484 | $post = Article::findOrFail($id); 485 | $post->title = $request->input('title'); 486 | $post->body = $request->input('body'); 487 | $post->published_at = $request->input('published_at'); 488 | $post->save(); 489 | } 490 | if($post){ 491 | return redirect('articles'); 492 | } 493 | } 494 | } 495 | 496 | 497 | 498 | //code 6.28 499 | {!! Form::password('password') !!} 500 | The same way, we could generate other inputs this way: 501 | {!! Form::email($name, $value = null, $attributes = array()) !!} 502 | {!! Form::file($name, $attributes = array()) !!} 503 | For Checkboxes and Radio Buttons we can go this way: 504 | {!! Form::checkbox('name', 'value') !!} 505 | {!! Form::radio('name', 'value') !!} 506 | We could generate a Checkbox or Radio Input that is "Checked". 507 | {!! Form::checkbox('name', 'value', true) !!} 508 | {!! Form::radio('name', 'value', true) !!} 509 | For the Drop-Down Lists we generally generate it this way: 510 | {!! Form::select('size', array('L' => 'Large', 'S' => 'Small')) !!} 511 | We can generate a Drop-Down list with selected default: 512 | {!! Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S') !!} 513 | For generating a Submit Button the process is quite simple: 514 | echo Form::submit('Click Me!'); 515 | 516 | 517 | 518 | 519 | //code 6.29 520 | @extends('layouts.app') 521 | 522 | @section('content') 523 |
    524 |
    Recent Tasks
    525 | 526 |
    527 |
    528 |
    529 |
    530 | Categories 531 |

    532 | Posts 533 |

    534 | Add Users 535 |

    536 | 537 |
    538 |
    539 |
    540 |
    541 |
    542 |
    543 | 544 | {{ csrf_field() }} 545 |
    546 | 549 | 550 |
    551 | @if($categories == null) 552 | 553 |
    554 | @endif 555 | @if($categories != null) 556 |
    557 | 558 | * 559 | 566 |
    567 | @endif 568 |
    569 | 570 | * 571 | 579 |
    580 | @if($writers == null) 581 | 583 |
    584 | @endif 585 | @if($writers != null) 586 |
    587 | 588 | * 589 | 590 | 597 |
    598 | @endif 599 | 600 |
    601 | 606 |
    607 |
    608 | 611 | 612 |
    613 |
    614 | 616 |
    617 | 618 |
    619 |
    620 |
    621 | 622 | 623 | @endsection 624 | 625 | 626 | 627 | 628 | 629 | 630 | //code 6.30 631 | /** 632 | * Show the form for creating a new resource. 633 | * 634 | * @return \Illuminate\Http\Response 635 | */ 636 | public function create() 637 | { 638 | if( Auth::user()->id == 1 ){ 639 | $categories = Category::where('user_id', Auth::user()->id)->get(); 640 | $writers = Writer::where('user_id', Auth::user()->id)->get(); 641 | return view('articlelead.create', compact('categories', 'writers')); 642 | } 643 | return view('auth.login'); 644 | 645 | 646 | } 647 | /** 648 | * Store a newly created resource in storage. 649 | * 650 | * @param \Illuminate\Http\Request $request 651 | * @return \Illuminate\Http\Response 652 | */ 653 | public function store(Request $request){ 654 | if(Auth::user()->id == 1){ 655 | if($file = $request->file('image')){ 656 | $name = $file->getClientOriginalName(); 657 | //using the Article model to create posts 658 | $post = ArticleLead::create([ 659 | 'title' => $request->input('title'), 660 | 'body' => $request->input('body'), 661 | 'user_id' => Auth::user()->id, 662 | 'category_id' => $request->input('category_id'), 663 | 'writer_id' => $request->input('writer_id'), 664 | 'tag' => $request->input('tag'), 665 | 'image' => $name 666 | ]); 667 | $file->move('images/articlesleadnews', $name); 668 | } 669 | if($post){ 670 | return redirect()->route('article.show', ['post'=> $post->id]) 671 | ->with('success', 'post created successfully'); 672 | } 673 | } 674 | return back()->withInput()->with('errors', 'Error creating new post'); 675 | } 676 | 677 | 678 | 679 | //code 6.31 680 | @extends('layouts.app') 681 | 682 | @section('content') 683 |
    684 |
    Recent Tasks
    685 | 686 |
    687 |
    688 |
    689 |
    690 | Categories 691 |

    692 | Posts 693 |

    694 | Add Users 695 |

    696 | 697 |
    698 |
    699 |
    700 |
    701 |
    702 |
    703 |
    705 | {{ csrf_field() }} 706 | 707 | 708 |
    709 | 712 | 714 |
    715 | @if($categories == null) 716 | 717 |
    718 | @endif 719 | 720 | @if($categories != null) 721 |
    722 | 723 | * 724 | 725 | 733 |
    734 | @endif 735 | 736 |
    737 | 738 | * 739 | 748 |
    749 | 750 | @if($writers == null) 751 | 753 |
    754 | @endif 755 | 756 | @if($writers != null) 757 |
    758 | 759 | * 760 | 761 | 769 |
    770 | @endif 771 | 772 |
    773 | 778 |
    779 | 780 |
    781 | 784 | 786 |
    787 | 788 |
    789 | 791 |
    792 | 793 | 794 |
    795 | 796 |
    797 |
    798 | 799 | 800 | 801 | @endsection 802 | 803 | 804 | 805 | //code 6.32 806 |
    808 | {{ csrf_field() }} 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | //code 6.33 819 | $ php artisan make:request StoreArticle 820 | 821 | 822 | 823 | 824 | 825 | //code 6.34 826 | public function rules() 827 | { 828 | return [ 829 | 'title' => 'required|unique:posts|max:255', 830 | 'body' => 'required', 831 | ]; 832 | 833 | public function authorize() 834 | { 835 | return true; 836 | } 837 | /** 838 | * Get the validation rules that apply to the request. 839 | * 840 | * @return array 841 | */ 842 | public function rules() 843 | { 844 | return [ 845 | 'title' => 'required|unique:users|max:255', 846 | 'body' => 'required', 847 | ]; 848 | } 849 | public function messages() 850 | { 851 | return [ 852 | 'title.required' => 'Title is required!', 853 | 'body.required' => 'Content is required!', 854 | ]; 855 | } 856 | 857 | 858 | 859 | 860 | 861 | -------------------------------------------------------------------------------- /chapter-ten: -------------------------------------------------------------------------------- 1 | //code 10.1 2 | $ composer create-project --prefer-dist laravel/laravel laranew 3 | 4 | 5 | //code 10.2 6 | ss@ss-H81M-S1:~/code/laranew$ php artisan -V 7 | Laravel Framework 5.8.4 8 | 9 | 10 | //code 10.3 11 | //app/User.php 12 | 'datetime', 49 | ]; 50 | } 51 | 52 | 53 | 54 | //code 10.4 55 | class User extends Authenticatable implements MustVerifyEmail 56 | 57 | 58 | //code 10.5 59 | public function up() 60 | { 61 | Schema::create('users', function (Blueprint $table) { 62 | $table->bigIncrements('id'); 63 | $table->string('name'); 64 | $table->string('email')->unique(); 65 | $table->timestamp('email_verified_at')->nullable(); 66 | $table->string('password'); 67 | $table->rememberToken(); 68 | $table->timestamps(); 69 | }); 70 | } 71 | 72 | 73 | 74 | 75 | //code 10.6 76 | mysql> show databases; 77 | +-----------------------+ 78 | | Database | 79 | +-----------------------+ 80 | | information_schema | 81 | | b2a | 82 | | firstnews | 83 | | imagery | 84 | | laravel55 | 85 | | laravelforartisans | 86 | | laravelforbeginning | 87 | | laravelmodelrelations | 88 | | laravelrelations | 89 | | laravelstarttofinish | 90 | | myappo | 91 | | mymvc | 92 | | mysql | 93 | | newdata | 94 | | news | 95 | | performance_schema | 96 | | practiceone | 97 | | prisma | 98 | | sys | 99 | | test | 100 | | twoprac | 101 | +-----------------------+ 102 | 21 rows in set (0.20 sec) 103 | 104 | mysql> use newdata; 105 | Reading table information for completion of table and column names 106 | You can turn off this feature to get a quicker startup with -A 107 | 108 | Database changed 109 | mysql> show tables; 110 | +-------------------+ 111 | | Tables_in_newdata | 112 | +-------------------+ 113 | | migrations | 114 | | password_resets | 115 | | users | 116 | +-------------------+ 117 | 3 rows in set (0.00 sec) 118 | 119 | mysql> select * from users; 120 | +----+--------+-------------------------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+ 121 | | id | name | email | email_verified_at | password | remember_token | created_at | updated_at | 122 | +----+--------+-------------------------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+ 123 | | 1 | ss | s@s.com | NULL | $2y$10$T3lZcOOCC5C/OIRpOSO2SefpKfhtJF8myWaLeLAgNU4nthruQ2Dgu | NULL | 2019-03-17 03:51:29 | 2019-03-17 03:51:29 | 124 | | 2 | sanjib | sanjib12sinha@gmail.com | 2019-03-17 03:59:08 | $2y$10$kv34GZLUvIZLt/UqprrjCuOeS22.dI9i0R73vAX5IVfdLOiDpDauu | NULL | 2019-03-17 03:54:22 | 2019-03-17 03:59:08 | 125 | +----+--------+-------------------------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+ 126 | 2 rows in set (0.01 sec) 127 | 128 | 129 | //code 10.7 130 | ss@ss-H81M-S1:~/code/laranew$ php artisan tinker 131 | Psy Shell v0.9.9 (PHP 7.2.15-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman 132 | >>> $user = new App\User; 133 | => App\User {#2925} 134 | >>> $user; 135 | => App\User {#2925} 136 | >>> $user = App\User::find(1); 137 | => App\User {#2933 138 | id: 1, 139 | name: "ss", 140 | email: "s@s.com", 141 | email_verified_at: null, 142 | created_at: "2019-03-17 03:51:29", 143 | updated_at: "2019-03-17 03:51:29", 144 | } 145 | >>> 146 | 147 | 148 | 149 | 150 | //code 10.8 151 | MAIL_DRIVER=log 152 | MAIL_HOST=smtp.mailtrap.io 153 | MAIL_PORT=2525 154 | MAIL_USERNAME=null 155 | MAIL_PASSWORD=null 156 | MAIL_ENCRYPTION=null 157 | 158 | 159 | 160 | //code 10.9 161 | //app/Http/Kearnel.php 162 | protected $routeMiddleware = [ 163 | 'auth' => \App\Http\Middleware\Authenticate::class, 164 | 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 165 | 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 166 | 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 167 | 'can' => \Illuminate\Auth\Middleware\Authorize::class, 168 | 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 169 | 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 170 | 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 171 | 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 172 | ]; 173 | The last line in the above code (10.9) is important here: ' 174 | 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class 175 | 176 | 177 | 178 | //code 10.10 179 | //routes/web.php 180 | Route::get('/', function () { 181 | return view('welcome'); 182 | })->middleware('verified'); 183 | Auth::routes(['verify' => true]); 184 | 185 | 186 | 187 | //code 10.11 188 | // storage/logs/laravel-2019-03-17.log 189 | Content-Type: text/plain; charset=utf-8 190 | Content-Transfer-Encoding: quoted-printable 191 | 192 | [Laravel](http://localhost) 193 | 194 | # Hello! 195 | 196 | Please click the button below to verify your email address. 197 | 198 | Verify Email Address: http://localhost:8000/email/verify/2?expires=1552798462&signature=80dd1cd1315533a03d03f66fb0e90b5a9b21c454257b6a7e96b4d5ed9059b2f1 199 | 200 | If you did not create an account, no further action is required. 201 | 202 | Regards,Laravel 203 | 204 | If you’re having trouble clicking the "Verify Email Address" button, copy and paste the URL below 205 | into your web browser: [http://localhost:8000/email/verify/2?expires=1552798462&signature=80dd1cd1315533a03d03f66fb0e90b5a9b21c454257b6a7e96b4d5ed9059b2f1](http://localhost:8000/email/verify/2?expires=1552798462&signature=80dd1cd1315533a03d03f66fb0e90b5a9b21c454257b6a7e96b4d5ed9059b2f1) 206 | 207 | © 2019 Laravel. All rights reserved. 208 | 209 | 210 | 211 | //code 10.12 212 | ss@ss-H81M-S1:~$ cd code/laranew/ 213 | ss@ss-H81M-S1:~/code/laranew$ php artisan tinker 214 | Psy Shell v0.9.9 (PHP 7.2.15-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman 215 | >>> $user = App\User::find(2); 216 | => App\User {#2932 217 | id: 2, 218 | name: "sanjib", 219 | email: "sanjib12sinha@gmail.com", 220 | email_verified_at: "2019-03-17 03:59:08", 221 | created_at: "2019-03-17 03:54:22", 222 | updated_at: "2019-03-17 03:59:08", 223 | } 224 | >>> 225 | Look at this line: 226 | “email_verified_at: "2019-03-17 03:59:08", 227 | 228 | 229 | 230 | 231 | //code 10.13 232 | // app/Providers/EventServiceProvider.php 233 | protected $listen = [ 234 | Registered::class => [ 235 | SendEmailVerificationNotification::class, 236 | ], 237 | ]; 238 | 239 | 240 | 241 | //code 10.14 242 | $ composer require guzzlehttp/guzzle 243 | 244 | 245 | 246 | //code 10.15 247 | //config/mail.php 248 | env('MAIL_DRIVER', 'smtp'), 267 | 268 | /* 269 | |-------------------------------------------------------------------------- 270 | | SMTP Host Address 271 | |-------------------------------------------------------------------------- 272 | | 273 | | Here you may provide the host address of the SMTP server used by your 274 | | applications. A default option is provided that is compatible with 275 | | the Mailgun mail service which will provide reliable deliveries. 276 | | 277 | */ 278 | 279 | 'host' => env('MAIL_HOST', 'smtp.mailtrap.io'), 280 | 281 | /* 282 | |-------------------------------------------------------------------------- 283 | | SMTP Host Port 284 | |-------------------------------------------------------------------------- 285 | | 286 | | This is the SMTP port used by your application to deliver e-mails to 287 | | users of the application. Like the host we have set this value to 288 | | stay compatible with the Mailgun e-mail application by default. 289 | | 290 | */ 291 | 292 | 'port' => env('MAIL_PORT', 465), 293 | 294 | /* 295 | |-------------------------------------------------------------------------- 296 | | Global "From" Address 297 | |-------------------------------------------------------------------------- 298 | | 299 | | You may wish for all e-mails sent by your application to be sent from 300 | | the same address. Here, you may specify a name and address that is 301 | | used globally for all e-mails that are sent by your application. 302 | | 303 | */ 304 | 305 | 'from' => [ 306 | 'address' => env('MAIL_FROM_ADDRESS', 'me@sanjib.site'), 307 | 'name' => env('MAIL_FROM_NAME', 'Sanjib Sinha'), 308 | ], 309 | 310 | /* 311 | |-------------------------------------------------------------------------- 312 | | E-Mail Encryption Protocol 313 | |-------------------------------------------------------------------------- 314 | | 315 | | Here you may specify the encryption protocol that should be used when 316 | | the application send e-mail messages. A sensible default using the 317 | | transport layer security protocol should provide great security. 318 | | 319 | */ 320 | 321 | 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 322 | 323 | /* 324 | |-------------------------------------------------------------------------- 325 | | SMTP Server Username 326 | |-------------------------------------------------------------------------- 327 | | 328 | | If your SMTP server requires a username for authentication, you should 329 | | set it here. This will get used to authenticate with your server on 330 | | connection. You may also set the "password" value below this one. 331 | | 332 | */ 333 | 334 | 'username' => env('MAIL_USERNAME'), 335 | 336 | 'password' => env('MAIL_PASSWORD'), 337 | 338 | /* 339 | |-------------------------------------------------------------------------- 340 | | Sendmail System Path 341 | 342 | 343 | 344 | 345 | //code 10.16 346 | //.env 347 | MAIL_DRIVER=smtp 348 | MAIL_HOST=smtp.mailtrap.io 349 | MAIL_PORT=465 350 | MAIL_USERNAME=************** 351 | MAIL_PASSWORD=************** 352 | MAIL_ENCRYPTION=null 353 | 354 | 355 | 356 | 357 | //code 10.17 358 | $ php artisan make:mail SendMailable 359 | 360 | 361 | 362 | //code 10.18 363 | //app/Mail/SendMailable.php 364 | name = $name; 386 | } 387 | 388 | /** 389 | * Build the message. 390 | * 391 | * @return $this 392 | */ 393 | public function build() 394 | { 395 | return $this->view('email.name'); 396 | } 397 | } 398 | 399 | 400 | 401 | //code 10.19 402 | //resources/views/email/name.blade.php 403 |
    404 | Hi, This is : {{ $name }} 405 |
    406 | 407 | 408 | 409 | 410 | //code 10.20 411 | //app/Http/Controllers/HomeController.php 412 | middleware('auth'); 431 | } 432 | 433 | /** 434 | * Show the application dashboard. 435 | * 436 | * @return \Illuminate\Contracts\Support\Renderable 437 | */ 438 | public function index() 439 | { 440 | return view('home'); 441 | } 442 | 443 | public function mail() 444 | { 445 | $name = 'Sanjib'; 446 | Mail::to('me@sanjib.site')->send(new SendMailable($name)); 447 | 448 | return 'Email was sent'; 449 | } 450 | } 451 | 452 | 453 | 454 | //code 10.21 455 | //routes/web.php 456 | Route::get('/send/email', 'HomeController@mail'); 457 | 458 | 459 | 460 | //code 10.22 461 |
    462 | Hi, This is : Sanjib 463 |
    464 | 465 | 466 | 467 | //code 10.23 468 | public function mail() 469 | { 470 | $name = 'Sanjib'; 471 | Mail::to('me@sanjib.site')->send(new SendMailable($name)); 472 | return 'Email was sent'; 473 | } 474 | //The ‘MailTrap raw output is like this: 475 | //code 10.24 476 | Message-ID: 477 | Date: Fri, 22 Mar 2019 03:34:08 +0000 478 | Subject: Send Mailable 479 | From: Sanjib Sinha 480 | To: me@sanjib.site 481 | MIME-Version: 1.0 482 | Content-Type: text/html; charset=utf-8 483 | Content-Transfer-Encoding: quoted-printable 484 | 485 |
    486 | Hi, This is : Sanjib 487 |
    488 | 489 | 490 | 491 | 492 | //code 10.25 493 | //.env 494 | MAIL_DRIVER=smtp 495 | MAIL_HOST=smtp.mailtrap.io 496 | MAIL_PORT=2525 497 | MAIL_USERNAME=***************** 498 | MAIL_PASSWORD=***************** 499 | MAIL_ENCRYPTION=tls 500 | Next, we will issue this command on our terminal. 501 | 502 | 503 | 504 | //code 10.26 505 | $ php artisan make:notification NewuserRegistered 506 | 507 | 508 | 509 | 510 | //code 10.27 511 | //app/Notifications/NewuserRegistered.php 512 | line('A new user just registered.') 556 | ->action('MyApp', url('/')) 557 | ->line('Thank you for using our application!'); 558 | } 559 | 560 | /** 561 | * Get the array representation of the notification. 562 | * 563 | * @param mixed $notifiable 564 | * @return array 565 | */ 566 | public function toArray($notifiable) 567 | { 568 | return [ 569 | // 570 | ]; 571 | } 572 | } 573 | 574 | 575 | 576 | 577 | //code 10.28 578 | //resources/web.php 579 | use App\Notifications\NewuserRegistered; 580 | use App\User; 581 | Route::get('/notify', function () { 582 | User::find(1)->notify(new NewuserRegistered); 583 | return view('notify'); 584 | }); 585 | 586 | 587 | 588 | //code 10.29 589 | [Laravel](http://localhost) 590 | 591 | # Hello! 592 | 593 | A new user just registered. 594 | 595 | MyApp: http://localhost:8000 596 | 597 | Thank you for using our application! 598 | 599 | Regards,Laravel 600 | 601 | If you’re having trouble clicking the "MyApp" button, copy and paste the URL below 602 | into your web browser: [http://localhost:8000](http://localhost:8000) 603 | 604 | © 2019 Laravel. All rights reserved. 605 | 606 | 607 | 608 | 609 | 610 | -------------------------------------------------------------------------------- /chapter-three: -------------------------------------------------------------------------------- 1 | //code 3.1 2 | //routes/web.php 3 | Route::get('/', function () { 4 | return view('welcome'); 5 | }); 6 | 7 | 8 | //code 3.2 9 | //routes/web.php 10 | Route::get('/', 'WelcomeController@index'); 11 | 12 | 13 | //code 3.3 14 | //routes/web.php 15 | Route::resource('tasks', 'TaskController'); 16 | 17 | //code 3.4 18 | $ php artisan route:list 19 | 20 | //code 3.5 21 | $ php artisan make:controller TaskController --resource 22 | //the output of code 3.5 23 | Controller created successfully. 24 | 25 | ------------ 26 | 27 | //code 3.6 28 | // app/Http/Controllers/TaskController.php 29 | middleware('auth')->except('index', 'show'); 143 | } 144 | /** 145 | * Display a listing of the resource. 146 | * 147 | * @return \Illuminate\Http\Response 148 | */ 149 | public function index() 150 | { 151 | // 152 | $tasks = Task::get(); 153 | return view('tasks.index', compact('tasks')); 154 | } 155 | 156 | /** 157 | * Show the form for creating a new resource. 158 | * 159 | * @return \Illuminate\Http\Response 160 | */ 161 | public function create() 162 | { 163 | // 164 | if(Auth::user()->is_admin == 1){ 165 | return view('tasks.create'); 166 | } 167 | else { 168 | return redirect('home'); 169 | } 170 | 171 | } 172 | 173 | /** 174 | * Store a newly created resource in storage. 175 | * 176 | * @param \Illuminate\Http\Request $request 177 | * @return \Illuminate\Http\Response 178 | */ 179 | public function store(Request $request) 180 | { 181 | // 182 | if(Auth::user()->is_admin == 1){ 183 | 184 | $post = new Task; 185 | $post->title = $request->input('title'); 186 | $post->body = $request->input('body'); 187 | $post->save(); 188 | 189 | if($post){ 190 | return redirect('tasks'); 191 | } 192 | 193 | } 194 | 195 | } 196 | 197 | /** 198 | * Display the specified resource. 199 | * 200 | * @param int $id 201 | * @return \Illuminate\Http\Response 202 | */ 203 | public function show($id) 204 | { 205 | // 206 | $task = Task::findOrFail($id); 207 | return view('tasks.show', compact('task')); 208 | } 209 | 210 | /** 211 | * Show the form for editing the specified resource. 212 | * 213 | * @param int $id 214 | * @return \Illuminate\Http\Response 215 | */ 216 | public function edit($id) 217 | { 218 | // 219 | if(Auth::user()->is_admin == 1){ 220 | $task = Task::findOrFail($id); 221 | return view('tasks.edit', compact('task')); 222 | } 223 | else { 224 | // code... 225 | return redirect('home'); 226 | } 227 | } 228 | 229 | /** 230 | * Update the specified resource in storage. 231 | * 232 | * @param \Illuminate\Http\Request $request 233 | * @param int $id 234 | * @return \Illuminate\Http\Response 235 | */ 236 | public function update(Request $request, $id) 237 | { 238 | // 239 | if(Auth::user()->is_admin == 1){ 240 | 241 | $post = Task::findOrFail($id); 242 | $post->title = $request->input('title'); 243 | $post->body = $request->input('body'); 244 | $post->save(); 245 | 246 | if($post){ 247 | return redirect('tasks'); 248 | } 249 | 250 | } 251 | 252 | } 253 | 254 | /** 255 | * Remove the specified resource from storage. 256 | * 257 | * @param int $id 258 | * @return \Illuminate\Http\Response 259 | */ 260 | public function destroy($id) 261 | { 262 | // 263 | } 264 | } 265 | 266 | ------------- 267 | 268 | //code 3.9 269 | //creating Task model and database table 270 | $ php artisan make:model Task -m 271 | Model created successfully. 272 | Created Migration: 2019_02_16_041652_create_tasks_table 273 | 274 | ------------ 275 | 276 | //code 3.10 277 | //app/Task.php 278 | increments('id'); 313 | $table->string('title'); 314 | $table->text('body'); 315 | $table->timestamps(); 316 | }); 317 | } 318 | 319 | /** 320 | * Reverse the migrations. 321 | * 322 | * @return void 323 | */ 324 | public function down() 325 | { 326 | Schema::dropIfExists('tasks'); 327 | } 328 | } 329 | 330 | ---------------- 331 | 332 | //code 3.12 333 | public function __construct() 334 | { 335 | $this->middleware('auth')->except('index', 'show'); 336 | } 337 | 338 | ------------ 339 | 340 | //code 3.13 341 | public function show($id) 342 | { 343 | // 344 | $task = Task::findOrFail($id); 345 | return view('tasks.show', compact('task')); 346 | } 347 | 348 | ------------ 349 | 350 | //code 3.14 351 | // resources/views/tasks/show.blade.php 352 |
    353 | {{ $task->title }} 354 |
    355 |
    356 | {{ $task->body }} 357 |
    358 | 359 | 360 | ------------- 361 | 362 | //code 3.15 363 | public function create() 364 | { 365 | // 366 | if(Auth::user()->is_admin == 1){ 367 | return view('tasks.create'); 368 | } 369 | else { 370 | return redirect('home'); 371 | } 372 | 373 | } 374 | 375 | ------------- 376 | 377 | //code 3.16 378 | // app/HTTP/Controllers/TaskController.php 379 | /** 380 | * Display a listing of the resource. 381 | * 382 | * @return \Illuminate\Http\Response 383 | */ 384 | public function index() 385 | { 386 | $tasks = Task::get(); 387 | return view('tasks.index', compact('tasks')); 388 | } 389 | 390 | 391 | --------------- 392 | 393 | //code 3.17 394 | Method | URI | Action | Route Name 395 | ----------|-----------------------|--------------|--------------------- 396 | GET | 'tasks' | index | tasks.index 397 | GET | 'tasks/create' | create | tasks.create 398 | POST | 'tasks' | store | tasks.store 399 | GET | 'tasks/{task}' | show | tasks.show 400 | GET | 'tasks/{task}/edit' | edit | tasks.edit 401 | PUT/PATCH | 'tasks/{task}' | update | tasks.update 402 | DELETE | 'tasks/{task}' | destroy | tasks.destroy 403 | 404 | --------------- 405 | 406 | //code 3.18 407 | Route::get('tasks/important', 'TaskController@important'); 408 | 409 | ---------------- 410 | 411 | //code 3.19 412 | Route::get('tasks/important', 'TaskController@important'); 413 | Route::resource('tasks', 'TaskController'); 414 | 415 | --------------- 416 | 417 | //code 3.20 418 | public function store(Request $request) 419 | { 420 | if(Auth::user()->is_admin == 1){ 421 | $post = new Task; $post->title = $request->input('title'); 422 | $post->body = $request->input('body'); 423 | $post->save(); 424 | 425 | if($post){ 426 | return redirect('tasks'); 427 | } 428 | } 429 | } 430 | 431 | 432 | ------------- 433 | 434 | //code 3.21 435 | public function update(Request $request, $id) 436 | { 437 | if(Auth::user()->is_admin == 1){ 438 | $post = Task::findOrFail($id); 439 | $post->title = $request->input('title'); 440 | $post->body = $request->input('body'); 441 | $post->save(); 442 | if($post){ 443 | return redirect('tasks'); 444 | } 445 | } 446 | } 447 | 448 | 449 | -------------- 450 | 451 | //code 3.22 452 | //app/Repositories/DBUserRepository.php 453 | users = $users; 483 | } 484 | …//code continues 485 | } 486 | 487 | 488 | ------------ 489 | 490 | //code 3.25 491 | //resources/views/tasks/index.blade.php 492 | @extends('layouts.app') 493 | @section('content') 494 |
    495 |
    496 |
    497 |
    498 |
    Task Page
    499 |
    500 | @foreach($tasks as $task) 501 | 502 | {{ $task->title }} 503 | 504 |

    505 | @endforeach 506 |
    507 |
    508 |
    509 |
    510 |
    511 | @endsection 512 | 513 | 514 | ---------------- 515 | 516 | //code 3.26 517 | 518 | 519 | App Name - @yield('title') 520 | 521 | 522 |
    523 | @yield('content') 524 |
    525 | 526 | 527 | The @yield('content') part uses our 'index.blade.php' page content. In the default 'app.blade.php' page, that part was included like this: 528 |
    529 | @yield('content') 530 |
    531 | 532 | 533 | ------------------- 534 | 535 | //code 3.27 536 | @foreach ($users as $user) 537 | @if ($user->admin == 1) 538 | @continue 539 | @endif 540 | 541 |
  • {{ $user->name }}
  • 542 | @if ($user->number == 1) 543 | @break 544 | @endif 545 | @endforeach 546 | We can also check whether the user is a registered member or not by this simple check-up method: 547 | //code 3.28 548 | @auth 549 | // The user is authenticated... 550 | @endauth 551 | 552 | @guest 553 | // The user is not authenticated... 554 | @endguest 555 | 556 | ----------------- 557 | 558 | //code 3.29 559 | @auth('admin') 560 | // The user is authenticated... 561 | @endauth 562 | 563 | @guest('admin') 564 | // The user is not authenticated... 565 | @endguest 566 | 567 | ---------------- 568 | 569 | //code 3.30 570 | @if (count($users) === 1) 571 | I have one user! 572 | @elseif (count($users) > 1) 573 | I have multiple users! 574 | @else 575 | I don't have any users! 576 | @endif 577 | 578 | 579 | ---------------- 580 | 581 | //code 3.31 582 | @unless (Auth::check()) 583 | You are not signed in. 584 | @endunless 585 | 586 | 587 | ---------------- 588 | 589 | //code 3.32 590 | @isset($images) 591 | // $images is defined and is not null... 592 | @endisset 593 | 594 | @empty($images) 595 | // $images is 'empty'... 596 | @endempty 597 | 598 | 599 | --------------- 600 | 601 | //code 3.33 602 | @foreach ($users as $user) 603 | @if ($user->id == 1) 604 | @continue 605 | @endif 606 | 607 |
  • {{ $user->name }}
  • 608 | 609 | @if ($user->id == 3) 610 | @break 611 | @endif 612 | @endforeach 613 | 614 | 615 | 616 | -------------------------------------------------------------------------------- /chapter-twelve: -------------------------------------------------------------------------------- 1 | //code 12.1 2 | $ composer create-project --prefer-dist laravel/laravel apilara 3 | 4 | 5 | //code 12.2 6 | //creating a controller 7 | $ php artisan make:controller ArticleController --resource 8 | Controller created successfully. 9 | 10 | 11 | 12 | //code 12.3 13 | $ php artisan make:model Article -m 14 | 15 | 16 | //code 12.4 17 | bigIncrements('id'); 34 | $table->string('title'); 35 | $table->text('body'); 36 | $table->timestamps(); 37 | }); 38 | } 39 | 40 | /** 41 | * Reverse the migrations. 42 | * 43 | * @return void 44 | */ 45 | public function down() 46 | { 47 | Schema::dropIfExists('articles'); 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | //code 12.5 55 | $ php artisan make:seeder ArticlesTableSeeder 56 | Seeder created successfully. 57 | 58 | 59 | //code 12.6 60 | $ php artisan make:factory ArticleFactory 61 | Factory created successfully 62 | 63 | 64 | //code 12.7 65 | //database/seeds/ ArticlesTableSeeder.php 66 | create(); 81 | } 82 | } 83 | 84 | 85 | 86 | //code 12.8 87 | // database/factories/ArticleFactory.php 88 | define(App\Article::class, function (Faker $faker) { 93 | return [ 94 | 'title' => $faker->text(50), 95 | 'body' => $faker->text(300) 96 | ]; 97 | }); 98 | 99 | 100 | 101 | //code 12.9 102 | $ php artisan db:seed 103 | Seeding: ArticlesTableSeeder 104 | Database seeding completed successfully. 105 | 106 | 107 | //code 12.10 108 | $ php artisan make:resource Article 109 | Resource created successfully. 110 | 111 | 112 | 113 | //code 12.13 114 | $ php artisan make:resource Article --collection 115 | $ php artisan make:resource ArticleCollection 116 | 117 | 118 | 119 | 120 | //code 12.14 121 | //app/Http/Resources/Article.php 122 | get('/user', function (Request $request) { 162 | return $request->user(); 163 | }); 164 | 165 | //list articles 166 | Route::get('articles', 'ArticleController@index'); 167 | //list single article 168 | Route::get('article/{id}', 'ArticleController@show'); 169 | //create new article 170 | Route::post('article', 'ArticleController@store'); 171 | //update articles 172 | Route::put('article', 'ArticleController@store'); 173 | //delete articles 174 | Route::delete('article/{id}', 'ArticleController@destroy'); 175 | 176 | 177 | 178 | //code 12.16 179 | //app/Http/Controllers/ArticleController.php 180 | $this->id, 244 | 'title' => $this->title, 245 | 'body' => $this->body 246 | ]; 247 | } 248 | } 249 | 250 | 251 | 252 | //code 12.18 253 | public function show($id) 254 | { 255 | //get article 256 | $article = Article::findOrFail($id); 257 | 258 | //returning single article as a resource 259 | return new ArticleResource($article); 260 | } 261 | 262 | 263 | 264 | //code 12.19 265 | use App\User; 266 | use App\Http\Resources\User as UserResource; 267 | Route::get('/user', function () { 268 | return new UserResource(User::find(1)); 269 | }); 270 | 271 | 272 | 273 | 274 | //code 12.20 275 | //app/Http/Resources/Article.php 276 | $this->id, 296 | 'title' => $this->title, 297 | 'body' => $this->body 298 | ]; 299 | } 300 | 301 | public function with($request) { 302 | 303 | return [ 304 | 'version' => '1.0.0', 305 | 'author_url' => url('https://sanjib.site') 306 | ]; 307 | 308 | } 309 | } 310 | 311 | 312 | 313 | 314 | //code 12.21 315 | $ php artisan make:resource UserCollection 316 | Resource collection created successfully. 317 | 318 | 319 | 320 | //code 12.22 321 | //app/Http/Resources/UserCollection.php 322 | public function toArray($request) 323 | { 324 | return parent::toArray($request); 325 | } 326 | 327 | 328 | 329 | //code 12.23 330 | //app/Http/Resources/UserCollection.php 331 | public function toArray($request) 332 | { 333 | return [ 334 | 'data' => $this->collection, 335 | 'links' => [ 336 | 'author_url' => 'https://sanjib.site', 337 | ], 338 | ]; 339 | } 340 | 341 | 342 | 343 | 344 | //code 12.24 345 | //routes/api.php 346 | use App\User; 347 | use App\Http\Resources\UserCollection; 348 | 349 | Route::get('/users', function () { 350 | return new UserCollection(User::all()); 351 | }); 352 | 353 | 354 | 355 | 356 | //code 12.25 357 | $ composer require laravel/passport 358 | 359 | - Installing psr/http-message (1.0.1): Downloading (100%) 360 | - Installing psr/http-factory (1.0.0): Downloading (100%) 361 | - Installing zendframework/zend-diactoros (2.1.1): Downloading (100%) 362 | - Installing symfony/psr-http-message-bridge (v1.2.0): Downloading (100%) 363 | - Installing phpseclib/phpseclib (2.0.15): Downloading (100%) 364 | - Installing defuse/php-encryption (v2.2.1): Downloading (100%) 365 | - Installing lcobucci/jwt (3.2.5): Downloading (100%) 366 | - Installing league/event (2.2.0): Downloading (100%) 367 | - Installing league/oauth2-server (7.3.3): Downloading (100%) 368 | - Installing ralouphie/getallheaders (2.0.5): Downloading (100%) 369 | - Installing guzzlehttp/psr7 (1.5.2): Downloading (100%) 370 | - Installing guzzlehttp/promises (v1.3.1): Downloading (100%) 371 | - Installing guzzlehttp/guzzle (6.3.3): Downloading (100%) 372 | - Installing firebase/php-jwt (v5.0.0): Downloading (100%) 373 | - Installing laravel/passport (v7.2.2): Downloading (100%) 374 | 375 | 376 | 377 | //code 12.26 378 | $ php artisan migrate 379 | Migrating: 2016_06_01_000001_create_oauth_auth_codes_table 380 | Migrated: 2016_06_01_000001_create_oauth_auth_codes_table 381 | Migrating: 2016_06_01_000002_create_oauth_access_tokens_table 382 | Migrated: 2016_06_01_000002_create_oauth_access_tokens_table 383 | Migrating: 2016_06_01_000003_create_oauth_refresh_tokens_table 384 | Migrated: 2016_06_01_000003_create_oauth_refresh_tokens_table 385 | Migrating: 2016_06_01_000004_create_oauth_clients_table 386 | Migrated: 2016_06_01_000004_create_oauth_clients_table 387 | Migrating: 2016_06_01_000005_create_oauth_personal_access_clients_table 388 | Migrated: 2016_06_01_000005_create_oauth_personal_access_clients_table 389 | 390 | 391 | 392 | //code 12.27 393 | $ php artisan passport:install 394 | Encryption keys generated successfully. 395 | Personal access client created successfully. 396 | Client ID: 1 397 | Client secret: CqTb7j2ABO1qAMM1OHInXodrpTtVkPxFuaR9UZs1 398 | Password grant client created successfully. 399 | Client ID: 2 400 | Client secret: LLxn4BP4Px4q4zJlt4u9JXGe3y4ghUIGQ4TqOv49 401 | 402 | 403 | 404 | //code 12.28 405 | //app/User.php 406 | 'datetime', 444 | ]; 445 | } 446 | 447 | 448 | 449 | //code 12.29 450 | //app/Providers/ AuthServiceProvider.php 451 | 'App\Policies\ModelPolicy', 469 | ]; 470 | 471 | /** 472 | * Register any authentication / authorization services. 473 | * 474 | * @return void 475 | */ 476 | public function boot() 477 | { 478 | $this->registerPolicies(); 479 | 480 | Passport::routes(); 481 | } 482 | } 483 | 484 | 485 | 486 | //code 12.30 487 | //config/auth.php 488 | 'guards' => [ 489 | 'web' => [ 490 | 'driver' => 'session', 491 | 'provider' => 'users', 492 | ], 493 | 494 | 'api' => [ 495 | 'driver' => 'passport', 496 | 'provider' => 'users', 497 | ], 498 | ], 499 | 500 | 501 | 502 | //code 12.31 503 | $ php artisan vendor:publish –tag=passport-components 504 | 505 | 506 | 507 | //code 12.32 508 | //resources/js/app.js 509 | Vue.component( 510 | 'passport-clients', 511 | require('./components/passport/Clients.vue').default 512 | ); 513 | 514 | Vue.component( 515 | 'passport-authorized-clients', 516 | require('./components/passport/AuthorizedClients.vue').default 517 | ); 518 | 519 | Vue.component( 520 | 'passport-personal-access-tokens', 521 | require('./components/passport/PersonalAccessTokens.vue').default 522 | ); 523 | 524 | 525 | //code 12.33 526 | //resources/views/home.blade.php 527 | @extends('layouts.app') 528 | 529 | @section('content') 530 |
    531 |
    532 |
    533 |
    534 |
    Dashboard
    535 | 536 |
    537 | @if (session('status')) 538 | 541 | @endif 542 | 543 | 544 | 545 | 546 |
    547 |
    548 |
    549 |
    550 |
    551 | @endsection 552 | 553 | 554 | 555 | //code 12.34 556 | $ php artisan passport:client 557 | 558 | Which user ID should the client be assigned to?: 559 | > 2 560 | 561 | What should we name the client?: 562 | > sanjib 563 | 564 | Where should we redirect the request after authorization? [http://localhost/auth/callback]: 565 | > 566 | 567 | New client created successfully. 568 | Client ID: 4 569 | Client secret: E02repG4eeeklfpaeX8i6YdtDi2tYQS6aYuKIO8I 570 | 571 | 572 | 573 | 574 | //code 12.35 575 | Route::get('/redirect', function () { 576 | $query = http_build_query([ 577 | 'client_id' => '5', 578 | 'redirect_uri' => 'https://sanjib.site/callback', 579 | 'response_type' => 'code', 580 | 'scope' => '', 581 | ]); 582 | 583 | return redirect('http://localhost:8000/oauth/authorize?'.$query); 584 | }); 585 | 586 | 587 | 588 | //code 12.36 589 | http://localhost:8000/oauth/authorize?client_id=5&redirect_uri=https%3A%2F%2Fsanjib.site%2Fcallback&response_type=code&scope= 590 | 591 | 592 | 593 | -------------------------------------------------------------------------------- /chapter-two: -------------------------------------------------------------------------------- 1 | 2 | //how to install Composer globally 3 | 4 | $ sudo apt-get update 5 | 6 | $ sudo apt-get install curl php-cli php-mbstring git unzip 7 | 8 | $ curl -sS https://getcomposer.org/installer -o composer-setup.php 9 | 10 | 11 | -------------------- 12 | 13 | $ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer 14 | 15 | 16 | ----------------- 17 | 18 | //code 2.1 19 | //installing Laravel with global composer 20 | composer create-project --prefer-dist laravel/laravel YourFirstLaravelProject 21 | 22 | //code 2.2 23 | composer create-project --prefer-dist laravel/laravel blog "5.7.*" 24 | 25 | //code 2.3 26 | //running Laravel 27 | $ php artisan serve 28 | 29 | //code 2.4 30 | //starting local web server in laravel 31 | $ php artisan serve 32 | 33 | 34 | //code 2.5 35 | //Taking application to the maintenance mode 36 | $ php artisan down 37 | 38 | //code 2.6 39 | //Clearing cache 40 | $ php artisan cache:clear 41 | 42 | 43 | //code 2.7 44 | $ cd /var/www/html 45 | 46 | //code 2.8 47 | $ sudo mkdir MyFirstLaravelProject 48 | 49 | //code 2.9 50 | $ sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 51 | 52 | //code 2.10 53 | $ sudo php composer-setup.php 54 | 55 | 56 | /code 2.11 57 | { 58 | "require": { 59 | "monolog/monolog": "1.0.*" 60 | } 61 | } 62 | 63 | //code 2.12 64 | $ php composer.phar install 65 | 66 | //code 2.13 67 | $ sudo composer create-project --prefer-dist laravel/laravel blog 68 | 69 | //code 2.14 70 | // installing VirtualBox on MAC/Linux 71 | $ sudo apt-get install virtualbox 72 | 73 | --------------------------------------- 74 | 75 | //code 2.15 76 | // installing Vagrant 77 | ss@ss-H81M-S1:~$ sudo apt-get install vagrant 78 | [sudo] password for ss: 79 | Reading package lists... Done 80 | Building dependency tree 81 | Reading state information... Done 82 | The following packages were automatically installed and are no longer required: 83 | gir1.2-keybinder-3.0 jsonlint libcdaudio1 libenca0 libjs-excanvas 84 | libkeybinder-3.0-0 libllvm5.0 libllvm5.0:i386 libmcrypt4 85 | libp11-kit-gnome-keyring:i386 libslv2-9 libsodium18 libvpx3:i386 mercurial 86 | mercurial-common php-cli-prompt php-composer-semver 87 | php-composer-spdx-licenses php-json-schema php-symfony-console 88 | php-symfony-filesystem php-symfony-finder php-symfony-process 89 | Use 'sudo apt autoremove' to remove them. 90 | The following additional packages will be installed: 91 | bsdtar bundler fonts-lato libgmp-dev libgmpxx4ldbl libruby2.3 rake ruby 92 | ruby-bundler ruby-childprocess ruby-dev ruby-did-you-mean ruby-domain-name 93 | ruby-erubis ruby-ffi ruby-http-cookie ruby-i18n ruby-listen ruby-log4r 94 | ruby-mime-types ruby-minitest ruby-molinillo ruby-net-http-persistent 95 | ruby-net-scp ruby-net-sftp ruby-net-ssh ruby-net-telnet ruby-netrc 96 | ruby-nokogiri ruby-power-assert ruby-rb-inotify ruby-rest-client 97 | ruby-sqlite3 ruby-test-unit ruby-thor ruby-unf ruby-unf-ext ruby2.3 98 | ruby2.3-dev rubygems-integration sqlite3 99 | Suggested packages: 100 | bsdcpio gmp-doc libgmp10-doc libmpfr-dev ri publicsuffix sqlite3-doc 101 | The following NEW packages will be installed: 102 | bsdtar bundler fonts-lato libgmp-dev libgmpxx4ldbl libruby2.3 rake ruby 103 | ruby-bundler ruby-childprocess ruby-dev ruby-did-you-mean ruby-domain-name 104 | ruby-erubis ruby-ffi ruby-http-cookie ruby-i18n ruby-listen ruby-log4r 105 | ruby-mime-types ruby-minitest ruby-molinillo ruby-net-http-persistent 106 | ruby-net-scp ruby-net-sftp ruby-net-ssh ruby-net-telnet ruby-netrc 107 | ruby-nokogiri ruby-power-assert ruby-rb-inotify ruby-rest-client 108 | ruby-sqlite3 ruby-test-unit ruby-thor ruby-unf ruby-unf-ext ruby2.3 109 | ruby2.3-dev rubygems-integration sqlite3 vagrant 110 | 0 upgraded, 42 newly installed, 0 to remove and 5 not upgraded. 111 | Need to get 9,248 kB of archives. 112 | After this operation, 44.8 MB of additional disk space will be used. 113 | Do you want to continue? [Y/n] y 114 | Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 fonts-lato all 2.0-1 [2,693 kB] 115 | Get:2 http://in.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 bsdtar amd64 3.1.2-11ubuntu0.16.04.4 [48.0 kB] 116 | Get:3 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 rubygems-integration all 1.10 [4,966 B] 117 | Get:4 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 rake all 10.5.0-2 [48.2 kB] 118 | …. 119 | 120 | 121 | --------------------------- 122 | 123 | //code 2.16 124 | $ vagrant -v 125 | //output 126 | Vagrant 2.2.3 127 | 128 | ------------ 129 | 130 | //code 2.17 131 | $ vagrant box add ubuntu/trusty64 132 | ==> box: Loading metadata for box 'ubuntu/trusty64' 133 | box: URL: https://vagrantcloud.com/ubuntu/trusty64 134 | ==> box: Adding box 'ubuntu/trusty64' (v20181218.1.0) for provider: virtualbox 135 | box: Downloading: https://vagrantcloud.com/ubuntu/boxes/trusty64/versions/20181218.1.0/providers/virtualbox.box 136 | ==> box: Successfully added box 'ubuntu/trusty64' (v20181218.1.0) for 'virtualbox'! 137 | 138 | --------- 139 | 140 | //code 2.18 141 | ss@ss-H81M-S1:~$ vagrant init ubuntu/trusty64 142 | A `Vagrantfile` has been placed in this directory. You are now 143 | ready to `vagrant up` your first virtual environment! Please read 144 | the comments in the Vagrantfile as well as documentation on 145 | `vagrantup.com` for more information on using Vagrant. 146 | ss@ss-H81M-S1:~$ vagrant up 147 | Bringing machine 'default' up with 'virtualbox' provider... 148 | ==> default: Importing base box 'ubuntu/trusty64'... 149 | ==> default: Matching MAC address for NAT networking... 150 | ==> default: Checking if box 'ubuntu/trusty64' is up to date... 151 | ==> default: Setting the name of the VM: ss_default_1547081879727_59147 152 | ==> default: Clearing any previously set forwarded ports... 153 | ==> default: Clearing any previously set network interfaces... 154 | ==> default: Preparing network interfaces based on configuration... 155 | default: Adapter 1: nat 156 | ==> default: Forwarding ports... 157 | default: 22 (guest) => 2222 (host) (adapter 1) 158 | ==> default: Booting VM... 159 | ==> default: Waiting for machine to boot. This may take a few minutes... 160 | default: SSH address: 127.0.0.1:2222 161 | default: SSH username: vagrant 162 | default: SSH auth method: private key 163 | default: 164 | default: Vagrant insecure key detected. Vagrant will automatically replace 165 | default: this with a newly generated keypair for better security. 166 | default: 167 | default: Inserting generated public key within guest... 168 | default: Removing insecure key from the guest if it's present... 169 | default: Key inserted! Disconnecting and reconnecting using new SSH key... 170 | ==> default: Machine booted and ready! 171 | ==> default: Checking for guest additions in VM... 172 | default: The guest additions on this VM do not match the installed version of 173 | default: VirtualBox! In most cases this is fine, but in rare cases it can 174 | default: prevent things such as shared folders from working properly. If you see 175 | default: shared folder errors, please make sure the guest additions within the 176 | default: virtual machine match the version of VirtualBox you have installed on 177 | default: your host and reload your VM. 178 | default: 179 | default: Guest Additions Version: 4.3.36 180 | default: VirtualBox Version: 5.1 181 | ==> default: Mounting shared folders... 182 | default: /vagrant => /home/ss 183 | ... 184 | 185 | ---------------------- 186 | 187 | //code 2.19 188 | $ sudo composer global require "laravel/homestead=~2.0" 189 | [sudo] password for ss: 190 | Changed current directory to /home/ss/.composer 191 | Do not run Composer as root/super user! See https://getcomposer.org/root for details 192 | ./composer.json has been created 193 | Loading composer repositories with package information 194 | Updating dependencies (including require-dev) 195 | Package operations: 6 installs, 0 updates, 0 removals 196 | - Installing symfony/process (v3.4.21): Downloading (100%) 197 | - Installing psr/log (1.1.0): Downloading (100%) 198 | - Installing symfony/debug (v4.2.2): Downloading (100%) 199 | - Installing symfony/polyfill-mbstring (v1.10.0): Downloading (100%) 200 | - Installing symfony/console (v3.4.21): Downloading (100%) 201 | - Installing laravel/homestead (v2.2.2): Downloading (100%) 202 | symfony/console suggests installing psr/log-implementation (For using the console logger) 203 | symfony/console suggests installing symfony/event-dispatcher () 204 | symfony/console suggests installing symfony/lock () 205 | Writing lock file 206 | Generating autoload files 207 | 208 | ------------------ 209 | 210 | //code 2.20 211 | // installing Homestead 212 | $ vagrant box add laravel/homestead 213 | ==> box: Loading metadata for box 'laravel/homestead' 214 | box: URL: https://vagrantcloud.com/laravel/homestead 215 | This box can work with multiple providers! The providers that it 216 | can work with are listed below. Please review the list and choose 217 | the provider you will be working with. 218 | 219 | 1) hyperv 220 | 2) parallels 221 | 3) virtualbox 222 | 4) vmware_desktop 223 | 224 | Enter your choice: 3 225 | ==> box: Adding box 'laravel/homestead' (v6.4.0) for provider: virtualbox 226 | box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/6.4.0/providers/virtualbox.box 227 | box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com 228 | ==> box: Successfully added box 'laravel/homestead' (v6.4.0) for 'virtualbox'! 229 | 230 | -------------------- 231 | 232 | //code 2.21 233 | $ cd ~ 234 | $ git clone https://github.com/laravel/homestead.git Homestead 235 | Cloning into 'Homestead'... 236 | remote: Enumerating objects: 22, done. 237 | remote: Counting objects: 100% (22/22), done. 238 | remote: Compressing objects: 100% (16/16), done. 239 | remote: Total 3232 (delta 14), reused 10 (delta 6), pack-reused 3210 240 | Receiving objects: 100% (3232/3232), 689.62 KiB | 926.00 KiB/s, done. 241 | Resolving deltas: 100% (1942/1942), done. 242 | Checking connectivity... done. 243 | 244 | --------------- 245 | 246 | //code 2.22 247 | //for Mac and Linux... 248 | $ bash init.sh 249 | //for Windows... 250 | init.bat 251 | 252 | ------------ 253 | 254 | //code 2.23 255 | $ cd ~/Homestead 256 | $ ls -la 257 | total 184 258 | drwxrwxr-x 9 ss ss 4096 Jan 10 07:07 . 259 | drwxr-xr-x 54 ss ss 4096 Jan 10 07:03 .. 260 | -rw-rw-r-- 1 ss ss 332 Jan 10 07:07 after.sh 261 | -rw-rw-r-- 1 ss ss 7669 Jan 10 07:07 aliases 262 | drwxrwxr-x 2 ss ss 4096 Jan 10 07:03 bin 263 | -rw-rw-r-- 1 ss ss 187 Jan 10 07:03 CHANGELOG.md 264 | -rw-rw-r-- 1 ss ss 853 Jan 10 07:03 composer.json 265 | -rw-rw-r-- 1 ss ss 82005 Jan 10 07:03 composer.lock 266 | -rw-rw-r-- 1 ss ss 213 Jan 10 07:03 .editorconfig 267 | drwxrwxr-x 8 ss ss 4096 Jan 10 07:03 .git 268 | -rw-rw-r-- 1 ss ss 14 Jan 10 07:03 .gitattributes 269 | drwxrwxr-x 2 ss ss 4096 Jan 10 07:03 .github 270 | -rw-rw-r-- 1 ss ss 154 Jan 10 07:03 .gitignore 271 | -rw-rw-r-- 1 ss ss 681 Jan 10 07:07 Homestead.yaml 272 | -rw-rw-r-- 1 ss ss 265 Jan 10 07:03 init.bat 273 | -rw-rw-r-- 1 ss ss 250 Jan 10 07:03 init.sh 274 | -rw-rw-r-- 1 ss ss 1077 Jan 10 07:03 LICENSE.txt 275 | -rw-rw-r-- 1 ss ss 383 Jan 10 07:03 phpunit.xml.dist 276 | -rw-rw-r-- 1 ss ss 1404 Jan 10 07:03 readme.md 277 | drwxrwxr-x 3 ss ss 4096 Jan 10 07:03 resources 278 | drwxrwxr-x 2 ss ss 4096 Jan 10 07:03 scripts 279 | drwxrwxr-x 4 ss ss 4096 Jan 10 07:03 src 280 | drwxrwxr-x 4 ss ss 4096 Jan 10 07:03 tests 281 | -rw-rw-r-- 1 ss ss 277 Jan 10 07:03 .travis.yml 282 | -rw-rw-r-- 1 ss ss 1878 Jan 10 07:03 Vagrantfile 283 | 284 | 285 | ----------------- 286 | 287 | //code 2.24 288 | ss@ss-H81M-S1:~/Homestead$ sudo gedit Homestead.yaml 289 | 290 | ------------- 291 | 292 | //code 2.25 293 | --- 294 | ip: "192.168.10.10" 295 | memory: 2048 296 | cpus: 1 297 | provider: virtualbox 298 | 299 | authorize: ~/.ssh/id_rsa.pub 300 | 301 | keys: 302 | - ~/.ssh/id_rsa 303 | 304 | folders: 305 | - map: ~/code 306 | to: /home/vagrant/code 307 | 308 | sites: 309 | - map: homestead.test 310 | to: /home/vagrant/code/public 311 | 312 | databases: 313 | - homestead 314 | 315 | # ports: 316 | # - send: 50000 317 | # to: 5000 318 | # - send: 7777 319 | # to: 777 320 | # protocol: udp 321 | 322 | # blackfire: 323 | # - id: foo 324 | # token: bar 325 | # client-id: foo 326 | # client-token: bar 327 | 328 | # zray: 329 | # If you've already freely registered Z-Ray, you can place the token here. 330 | # - email: foo@bar.com 331 | # token: foo 332 | # Don't forget to ensure that you have 'zray: "true"' for your site. 333 | 334 | 335 | ----------------------- 336 | 337 | folders: 338 | - map: ~/code 339 | to: /home/vagrant/code 340 | 341 | sites: 342 | - map: homestead.test 343 | to: /home/vagrant/code/public 344 | 345 | databases: 346 | - homestead 347 | 348 | ------------------ 349 | 350 | //code 2.26 351 | // Homestead.yaml 352 | --- 353 | ip: "192.168.10.10" 354 | memory: 2048 355 | cpus: 1 356 | provider: virtualbox 357 | 358 | authorize: ~/.ssh/id_rsa.pub 359 | 360 | keys: 361 | - ~/.ssh/id_rsa 362 | 363 | folders: 364 | - map: ~/code 365 | to: /home/vagrant/code 366 | 367 | - map: ~/code 368 | to: /home/vagrant/code 369 | 370 | sites: 371 | - map: test.localhost 372 | to: /home/vagrant/code/blog/public 373 | 374 | - map: my.local 375 | to: /home/vagrant/code/larastartofinish/public 376 | 377 | databases: 378 | - homestead 379 | - myappo 380 | 381 | 382 | ------------------------ 383 | 384 | /code 2.27 385 | //editing /etc/hosts file 386 | $ sudo gedit /etc/hosts 387 | 388 | //output of code 2.27 389 | 127.0.0.1 localhost 390 | ::1 ip6-localhost ip6-loopback 391 | 127.0.1.1 ss-H81M-S1 392 | 127.0.0.1 sandbox.dev 393 | 394 | # The following lines are desirable for IPv6 capable hosts 395 | ::1 ip6-localhost ip6-loopback 396 | fe00::0 ip6-localnet 397 | ff00::0 ip6-mcastprefix 398 | ff02::1 ip6-allnodes 399 | ff02::2 ip6-allrouters 400 | 401 | ---------------- 402 | 403 | //code 2.28 404 | $ vagrant up --provision 405 | 406 | ---------------- 407 | 408 | //code 2.30 409 | - map: ~/code 410 | to: /home/vagrant/code 411 | 412 | - map: ~/code 413 | to: /home/vagrant/code 414 | 415 | sites: 416 | - map: test.localhost 417 | to: /home/vagrant/code/blog/public 418 | 419 | - map: my.local 420 | to: /home/vagrant/code/larastartofinish/public 421 | 422 | ----------------- 423 | 424 | //code 2.31 425 | ss@ss-H81M-S1:~/Homestead$ vagrant up --provision 426 | Bringing machine 'homestead-7' up with 'virtualbox' provider... 427 | ==> homestead-7: Checking if box 'laravel/homestead' version '6.4.0' is up to date... 428 | ==> homestead-7: Clearing any previously set forwarded ports... 429 | ==> homestead-7: Vagrant has detected a configuration issue which exposes a 430 | ==> homestead-7: vulnerability with the installed version of VirtualBox. The 431 | ==> homestead-7: current guest is configured to use an E1000 NIC type for a 432 | ==> homestead-7: network adapter which is vulnerable in this version of VirtualBox. 433 | ==> homestead-7: Ensure the guest is trusted to use this configuration or update 434 | ==> homestead-7: the NIC type using one of the methods below: 435 | ==> homestead-7: 436 | ==> homestead-7: https://www.vagrantup.com/docs/virtualbox/configuration.html#default-nic-type 437 | ==> homestead-7: https://www.vagrantup.com/docs/virtualbox/networking.html#virtualbox-nic-type 438 | ==> homestead-7: Clearing any previously set network interfaces... 439 | ==> homestead-7: Preparing network interfaces based on configuration... 440 | homestead-7: Adapter 1: nat 441 | homestead-7: Adapter 2: hostonly 442 | ==> homestead-7: Forwarding ports... 443 | homestead-7: 80 (guest) => 8000 (host) (adapter 1) 444 | homestead-7: 443 (guest) => 44300 (host) (adapter 1) 445 | homestead-7: 3306 (guest) => 33060 (host) (adapter 1) 446 | homestead-7: 4040 (guest) => 4040 (host) (adapter 1) 447 | homestead-7: 5432 (guest) => 54320 (host) (adapter 1) 448 | homestead-7: 8025 (guest) => 8025 (host) (adapter 1) 449 | homestead-7: 27017 (guest) => 27017 (host) (adapter 1) 450 | homestead-7: 22 (guest) => 2222 (host) (adapter 1) 451 | ==> homestead-7: Running 'pre-boot' VM customizations... 452 | ==> homestead-7: Booting VM... 453 | ==> homestead-7: Waiting for machine to boot. This may take a few minutes... 454 | homestead-7: SSH address: 127.0.0.1:2222 455 | ... 456 | 457 | --------------------- 458 | 459 | //code 2.32 460 | ss@ss-H81M-S1:~/Homestead$ vagrant ssh 461 | Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-38-generic x86_64) 462 | 463 | * FYI Vagrant v2.2.2 & Virtualbox: 464 | * https://twitter.com/HomesteadDev/status/1071471881256079362 465 | 466 | 259 packages can be updated. 467 | 73 updates are security updates. 468 | Last login: Sat Jan 12 05:18:55 2019 from 10.0.2.2 469 | vagrant@homestead:~$ cd code/larastartofinish/ 470 | 471 | -------------------- 472 | 473 | //code 2.33 474 | vagrant@homestead:~$ mysql -u homestead -p 475 | Enter password: 476 | Welcome to the MySQL monitor. Commands end with ; or \g. 477 | Your MySQL connection id is 5 478 | Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu) 479 | 480 | Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 481 | 482 | Oracle is a registered trademark of Oracle Corporation and/or its 483 | affiliates. Other names may be trademarks of their respective 484 | owners. 485 | 486 | Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 487 | 488 | mysql> show databases; 489 | +--------------------+ 490 | | Database | 491 | +--------------------+ 492 | | information_schema | 493 | | homestead | 494 | | larastartofinish | 495 | | myappo | 496 | | mysql | 497 | | performance_schema | 498 | | socket_wrench | 499 | | sys | 500 | +--------------------+ 501 | 8 rows in set (0.08 sec) 502 | 503 | mysql> 504 | 505 | 506 | ---------------- 507 | 508 | //code 2.34 509 | mysql> exit 510 | Bye 511 | vagrant@homestead:~$ exit 512 | logout 513 | Connection to 127.0.0.1 closed. 514 | ss@ss-H81M-S1:~/Homestead$ vagrant halt 515 | ==> homestead-7: Attempting graceful shutdown of VM... 516 | ss@ss-H81M-S1:~/Homestead$ 517 | 518 | 519 | ----------------- 520 | 521 | //code 2.35 522 | $ composer create-project --prefer-dist laravel/laravel larastartofinish 523 | 524 | ----------------- 525 | 526 | //code 2.36 527 | $ sudo rm -rf vendor/ composer.lock 528 | 529 | ----------------- 530 | 531 | //code 2.37 532 | $ composer install 533 | 534 | --------------- 535 | 536 | //code 2.38 537 | vagrant@homestead:~$ cd code/larastartofinish/ 538 | 539 | --------------- 540 | 541 | //code 2.39 542 | vagrant@homestead:~/code/larastartofinish$ php artisan make:controller ArticleController --resource –model=Article 543 | Controller created successfully. 544 | 545 | ---------------- 546 | 547 | 548 | -------------------------------------------------------------------------------- /errata.md: -------------------------------------------------------------------------------- 1 | # Errata for *Book Title* 2 | 3 | On **page xx** [Summary of error]: 4 | 5 | Details of error here. Highlight key pieces in **bold**. 6 | 7 | *** 8 | 9 | On **page xx** [Summary of error]: 10 | 11 | Details of error here. Highlight key pieces in **bold**. 12 | 13 | *** --------------------------------------------------------------------------------