Successfully reported this slideshow.
Your SlideShare is downloading. ×

Getting to know Laravel 5

Upcoming SlideShare
Why Laravel?
Why Laravel?
Loading in …3

Check these out next

1 of 29 Ad

More Related Content

Slideshows for you (20)

Similar to Getting to know Laravel 5 (20)


Recently uploaded (20)

Getting to know Laravel 5

  1. 1. LARAVEL 5
  2. 2. What is Laravel? • PHP Framework • Most popular on Github • MVC Architecture • MIT License
  3. 3. Why Laravel? • Composer • Community • Code Construction
  4. 4. @bukhorimuhammad Less Talking, 
 More Coding
  5. 5. Requirements • Composer • Local server (with XAMPP, WAMP, MAMP, etc) • PHP 5.4+ • Mcrypt, OpenSSL, Mbstring, Tokenizer & PHP JSON extension
  6. 6. COMPOSER Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you. UNIX : curl -sS | php WIN :
  7. 7. Installing Laravel Composer Global : composer create-project laravel/laravel —- prefer-dist foldername Composer Local : php /path/to/composer.phar create-project laravel/laravel --prefer-dist foldername
  8. 8. Fix Folder Permission • Set storage folder to be writable (777) • Set vendor folder to be writable (777)
  9. 9. @bukhorimuhammad Laravel Concepts
  10. 10. Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component. php artisan list : 
 list all available artisan command php artisan help [command] : 
 help information for each command
  11. 11. Laravel Routing Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request. Route::get('/', function() { return 'Hello World'; }); Route::post('foo/bar', function() { return 'Hello World'; }); Route::any('foo', function() { return 'Hello World'; }); Route::match(['put', 'delete'], '/', function() { return 'Hello World'; });
  12. 12. HTTP METHODS • GET : used to retrieve (or read) a representation of a resource. Return 200 OK or 404 NOT FOUND or 400 BAD REQUEST • POST : used to create new resources. Return 201 OK or 404 NOT FOUND • PUT : used to update existing resource. Return 200 OK or 204 NO CONTENT or 404 NOT FOUND • DELETE : used to delete existing resource. Return 200 OK or 404 NOT FOUND
  13. 13. CODING TIME! • Open app/Http/routes.php • Insert couple of routes Route::get('tesget', function() { return 'Hello World'; }); Route::post('tespost', function() { return 'Hello World'; }); Route::any('tesany', function() { return 'Hello World'; }); Route::match(['put', 'delete'], 
 'tesmatch', function() { return 'Hello World'; });
  14. 14. CODING TIME! • Insert -> Route::resource(‘welcome’, ‘WelcomeController'); • Go to terminal / command line. • Type php artisan route:list
  15. 15. Laravel Controller Instead of defining all of your request handling logic in a single routes.php file, you may wish to organize this behavior using Controller classes. Controllers can group related HTTP request handling logic into a class. Controllers are typically stored in the app/Http/Controllers directory.
  16. 16. CODING TIME! • go to app/Http/Controller • create new Controller class (DummyController.php) • go to app/Http/routes.php • insert -> Route::get(‘dummy’, ‘DummyController@index‘);
  17. 17. CODING TIME! • php artisan make:controller Dummy2Controller • go to app/Http/routes.php • insert -> Route::resource(‘dummy2’, ‘Dummy2Controller');
  18. 18. Laravel Model A Model should contain all of the Business Logic of your application. Or in other words, how the application interacts with the database. The database configuration file is config/database.php DB::select('select * from users where id = :id', ['id' => 1]); DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']); DB::update('update users set votes = 100 where name = ?', ['John']); DB::delete('delete from users'); DB::statement('drop table users');
  19. 19. Query Builder The database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application, and works on all supported database systems. DB::table('users')->get(); DB::table('users')->insert( ['email' => '', 'votes' => 0] ); DB::table('users') ->where('id', 1) ->update(['votes' => 1]); DB::table('users')->where('votes', '<', 100)->delete();
  20. 20. Eloquent ORM The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. class User extends Model {} php artisan make:model User class User extends Model { protected $table = 'my_users'; } $users = User::all(); $model = User::where('votes', '>', 100)->firstOrFail();
  21. 21. Schema Builder The Laravel Schema class provides a database agnostic way of manipulating tables. It works well with all of the databases supported by Laravel, and has a unified API across all of these systems. Schema::create('users', function($table) { $table->increments('id'); }); Schema::rename($from, $to); Schema::drop('users');
  22. 22. Migrations & Seeding Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema. php artisan make:migration create_users_table class UserTableSeeder extends Seeder { public function run() { DB::table('users')->delete(); User::create(['email' => '']); } } php artisan db:seed php artisan migrate --force
  23. 23. CODING TIME! • go to env.example, rename it to .env • set DB_HOST=localhost • set DB_DATABASE=yourdbname • set DB_USERNAME=yourdbusername • set DB_PASSWORD=yourdbpassword
  24. 24. CODING TIME! run php artisan make:migration create_sample_table run php artisan migrate
  25. 25. CODING TIME! create SampleTableSeeder.php run php artisan db:seed run composer dump-autoload
  26. 26. CODING TIME! run php artisan make:model Sample run composer migrate edit app/Sample.php
  27. 27. Laravel View Views contain the HTML served by your application, and serve as a convenient method of separating your controller and domain logic from your presentation logic. Views are stored in the resources/views directory.
  28. 28. CODING TIME! create resources/views/sample.php create app/Http/controllers/SampleController.php register the route in app/Http/routes.php