ebook img

Learning Laravel 5 PDF

292 Pages·2016·6.01 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learning Laravel 5

Learning Laravel 5 Building Practical Applications Nathan Wu © 2015 - 2016 Nathan Wu Contents Book Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Book Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 What You Will Get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Book Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 1 - Installing Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2 - Building Our First Website . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 3 - Building A Support Ticket System . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 4 - Building A Blog Application . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 5 - Deploying Our Laravel Applications . . . . . . . . . . . . . . . . . . . . . 3 Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Book Status, Changelog and Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Current Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Learning Laravel 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Chapter 1: Installing Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Introducing CLI (Command Line Interface) . . . . . . . . . . . . . . . . . . . . . . . . . 7 CLI for MAC OSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CLI for Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CLI for Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Installing Laravel Using Homestead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 What is Homestead? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 How to install Homestead? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Install Homestead Using Method 1 (required Composer) . . . . . . . . . . . . . . . . . 9 Install Homestead Using Method 2 (requires Git) . . . . . . . . . . . . . . . . . . . . . 19 Configure Homestead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Launching Homestead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Installing Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 CONTENTS Chapter 2: Building Our First Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Exploring Laravel structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Understand routes.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Changing Laravel home page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Adding more pages to our first website . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Create our first controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Using our first controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Create other pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Integrating Twitter Bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Using Bootstrap CDN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Using Precompiled Bootstrap Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Using Bootstrap Source Code (Less) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Adding Twitter Bootstrap components . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Learning Blade templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Creating a master layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Extending the master layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Using other Bootstrap themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Refine our website layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Changing the navbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Changing the home page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Chapter 2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Chapter 3: Building A Support Ticket System . . . . . . . . . . . . . . . . . . . . . . . . . 82 What do we need to get started? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 What will we build? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Laravel Database Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Create a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Default database information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Create a database using the CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Create a database on Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Create a database on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Using Migrations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Meet Laravel Artisan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Create a new migration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Understand Schema to write migrations . . . . . . . . . . . . . . . . . . . . . . . . . 92 Create a new Eloquent model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Create a page to submit tickets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Create a view to display the submit ticket form . . . . . . . . . . . . . . . . . . . . . . 97 Install Laravel Collective packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Install a package using Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Create a service provider and aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 How to use HTML package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 A note about Laravel 5.2’s changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 CONTENTS Solution 1: use web middleware group . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Solution 1: fix the $errors object’s error . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Best solution: update your Kernel.php file . . . . . . . . . . . . . . . . . . . . . . . . . 109 Submit the form data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Using .env file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 What is the .env file? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 How to edit it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Insert data into the database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 View all tickets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 View a single ticket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Using a helper function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Edit a ticket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Delete a ticket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Sending an email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Sending emails using Gmail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Sending emails using Sendgrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Sending a test email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Sending an email when there is a new ticket . . . . . . . . . . . . . . . . . . . . . . . 146 Reply to a ticket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Create a new comments table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Introducing Eloquent: Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Create a new Comment model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Create a new comments controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Create a new CommentFormRequest . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Create a new reply form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Display the comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Chapter 3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Chapter 4: Building A Blog Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 What do we need to get started? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 What will we build? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Building a user registration page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Creating a logout functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Creating a login page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Add authentication throttling to your application . . . . . . . . . . . . . . . . . . . . . . 175 Building an admin area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 List all users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 All about Middleware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Creating a new middleware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Adding roles and permission to our app using Entrust . . . . . . . . . . . . . . . . . . . 188 Installing Entrust package for Laravel 5.2 (official package) . . . . . . . . . . . . . . . 188 Installing Entrust package for Laravel 5.2 (different branch) . . . . . . . . . . . . . . . 192 Installing Entrust package (for Laravel 5.1) . . . . . . . . . . . . . . . . . . . . . . . . 196 CONTENTS Create Entrust roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Assign roles to users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Restrict access to Manager users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Create an admin dashboard page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Create a new post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Create a Many-to-Many relation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Create and view categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Select categories when creating a post . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 View and edit posts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Display all posts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Edit a post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Display all blog posts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Display a single blog post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Using Polymorphic Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Seeding our database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Chapter 4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Chapter 5: Deploying Our Laravel Applications . . . . . . . . . . . . . . . . . . . . . . . . 269 Deploying your apps on shared hosting services . . . . . . . . . . . . . . . . . . . . . . . 269 Deploying on Godaddy shared hosting . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Deploying your apps using DigitalOcean . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Deploy a new Ubuntu server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Install Nginx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Install MySQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Install Nginx, PHP and other packages . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Install Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Possible Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Take a snapshot of your application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Little tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Chapter 5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Book Information 1 Book Description Learning Laravel 5: Building Practical Applications is the easiest way to learn web development using Laravel. Throughout 5 chapters, instructor Nathan Wu will teach you how to build many real- world applications from scratch. This bestseller is also completely about you. It has been structured very carefully, teaching you all you need to know from installing your Laravel 5 app to deploying it to a live server. When you have completed this book you will have created a dynamic website and have a good knowledge to become a good web developer. We first start with the basics. You will learn some main concepts and create a simple website. After that we progress to building more advanced web applications. Learn by doing! If you’re looking for a genuinely effective book that helps you to build your next amazing applications, this is the number one book for you. Requirements The projects in this book are intended to help people who have grasped the basics of PHP and HTML to move forward, developing more complex projects, using Laravel advanced techniques. The fundamentals of the PHP are not covered, you will need to: • Have a basic knowledge of PHP, HTML, CSS. • Love Laravel, like we do. What You Will Get • Lifetime access to the online book. (Read 70% of the book for FREE!) • Digital books: PDF, MOBI, EPUB (Premium Only) • Full source code (Premium Only) • Access new chapters of the book while it’s being written (Premium Only) • A community of 10000+ students learning together. • Amazing bundles and freebies to help you become a successful developer. • iPhone, iPad and Android Accessibility. 2 Book Description 3 Book Structure Note: This book is still under active development, that means some chapters and its content may change. The book also may have some errors and bugs. For any feedback, please send us an email. Thank you. Chapter 1 - Installing Laravel There are many ways to install Laravel. In this chapter, you will learn how to setup Laravel Homestead (a Vagrant-based virtual machine), and run your Laravel projects on it. Chapter 2 - Building Our First Website This book is meant to help you build the skills to create web applications as quickly and reliably as possible. We present you with four projects in various states of completion to explain and practice the various concepts being presented. Our first app, which is a simple website, will walk you through the structure of a Laravel app, and show some main concepts of Laravel. You will also create a good template for our next applications. Chapter 3 - Building A Support Ticket System After having a good template, we will start building a support ticket system to learn some Laravel features, such as Eloquent ORM, Eloquent Relationships, Migrations, Requests, Laravel Collective, sending emails, etc. While the project design is simple, it provides an excellent way to explore Laravel. You will also know how to construct your app structure the right way. Chapter 4 - Building A Blog Application Throughout the projects in this book up to this point, we’ve learned many things. It’s time to use our skills to build a complete blog system. You will learn to make an admin control panel to create and manage your posts, users, roles, permissions, etc. Chapter 5 - Deploying Our Laravel Applications Finally, we learn how to create our own web server and deploy our Laravel app to it. Launching your first Laravel 5 application is that easy! Book Description 4 Feedback Feedback from our readers is always welcome. Let us know what you liked or may have disliked. Simply send an email to

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.