Laravel 8 Tutorial for Beginner | Create Project

Laravel 8 Tutorial for Beginner | Create Project

 Laravel 8 Tutorial for Beginner | Create Project


In this Laravel tutorial, we will learn how to install Laravel and how to create your first app in Laravel for beginners. Now you can learn Laravel easily without the need to watch the Laravel video series or Laravel video tutorials. This Laravel tutorial will explain all the necessary concepts in easy language for you to learn Laravel easily and understand it better.

Introduction

Laravel is an elegant, expressive, and flexible PHP framework with an extreme focus on clean code and speed which describes itself as “The PHP framework for web artisans”. It is a free and Open source PHP framework created by Taylor Otwell, based on the Model View Controller (MVC) architecture.

Creating a web application from scratch can be daunting especially if you are a beginner. A simple web application also contains various small and big pieces and creating those pieces every time you are creating a web app can be boring and repetitive and there is no point in reinventing wheels. That’s when Laravel comes to your rescue.

What you should know prior to using our Laravel tutorial?

  • HTML/CSS (Duh!)
  • Basic understanding of core PHP
  • Intermediate PHP – This is sort of optional but if you have time, do learn some intermediate concepts like PHP OOP, abstraction, etc.
  • Basic Understanding of MVC Framework
  • Perseverance – Even though it’s easy to learn Laravel, it will test your patience at various intervals. At least I had some roadblocks because I knew PHP but I had no knowledge about frameworks. Even while learning the framework, or successfully completing projects, I was having confusion about the basic underlying concepts of the MVC Framework. But I didn’t give up.
  • Passion – C’mon Web development is fun! At least Laravel made it enjoyable. It’s best to enjoy the learning journey.

Installation and Configuration

Laravel offers various ways to install in windows or mac. The best and easiest way to install Laravel is through Composer. Composer is a dependency manager for PHP which you can install on your web server.

Prerequisites for installing Laravel 8

Before installing Laravel on your local platform (Localhost) you need to install the following programs:

  • Web Server – Apache or nginx
  • >= PHP 7.3
  • Some PHP extensions which might be pre-installed:
    1. BCMath PHP Extension
    2. Ctype PHP Extension
    3. Fileinfo PHP
    4. JSON PHP Extension
    5. Mbstring PHP Extension
    6. OpenSSL PHP Extension
    7. PDO PHP Extension
    8. Tokenizer PHP Extension
    9. XML PHP Extension
  • MySQL (Or Other Database sources, You can even use SQLite too)
  • Composer
  • An IDE will be really helpful for Laravel development. I recommend the VS Code or Atom. Both are free to use.

Steps to Install Laravel in your Localhost:

download https://getcomposer.org/

The best way to learn to program is to practice. Thus, here we will be learning basic concepts of Laravel by developing a simple To-Do Laravel Web App which will do the below functions with the help of this Laravel tutorial.

  • You can register and login to the web app
  • You can add tasks to your to-do list
  • You can edit as well as delete those tasks
  • Your list is only visible to you thus it implements authentication using email id and password

Exploring Directory Structure

Laravel applications follow the Model-View-Controller architecture design pattern.

laravel mvc structure


  • Models represent the entities in the database and help you to query the database and return the data
  • Views are the pages that will be displayed when accessed the app. View Component is used for the User Interface of the application.
  • Controllers handle user requests, get required data from the models, and pass them to the Views. Controllers act as an intermediary between Model and View Components to process the business logic and incoming request.

When you installed composer and created your first Laravel web app, you might have noticed the app folder with different files and folders. I know if you are a beginner, you may have a lot of questions about what are these folders for, etc., etc.

Let’s understand some

  • App: This directory is the meat of the application and contains the core code.
    • Console: This directory contains all the custom Artisan commands created using make: command
    • Exceptions: This directory contains the application’s exception handler and is a good place to add custom exception classes to handle different exceptions thrown by your application
    • Http: This directory contains all your controllers, middleware, and requests
    • Models, This is a new director added since Laravel 8 to hold Model files. Earlier, models were stored in the App folder, but now it can be stored inside App/Models folders too.
    • Providers: This directory contains all your service providers for the application. You can know more about service providers here
  • Bootstrap: This directory contains framework bootstrap as well as configuration files. It also contains the Cache directory which contains framework generated cache files
  • Config: This directory contains all your application’s configuration files.
  • Database: This directory contains all database migrations and seeds. You can also store SQLite database file here
  • Public: This directory contains assets like images, js files, and CSS.
  • Resources: This directory contains all view files and CSS or LESS or SASS files. It also contains a lang directory to store language files.
  • Routes: This directory contains all route definitions for the application. PHP is the file that receives all the requests to your application and here you can redirect the requests to their respective controller methods.
  • Storage: This directory contains blade templates, session files, cache files, and others.
  • Tests: This directory contains all the test files
  • Vendor: This directory contains all composer dependencies

Steps to create your first Laravel Application using Laravel tutorial

1) Create Your Project:

If you didn’t create your project in the installation section, create now by executing the below command:

composer create-project --prefer-dist laravel/laravel Login_laravel

2) Configure Database:

We need a database for our application, so it’s best to configure our database before doing anything. Laravel supports the following 4 databases −

  • MySQL
  • Postgres
  • SQLite
  • SQL Server
  •  In the file you will find code like below:

For this example, we will use SQLite as it is easy to configure and to use and you don’t have to install anything apart from creating just one empty file.

For other databases, you need to have that database installed in your system and then you can configure accordingly.

Laravel provides config/database.php to config database but it's better not to store database credentials there instead you can use a .env file where you can different types of credentials and other data.

Laravel comes with a default .env file at the root.

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=#Your database name
DB_USERNAME=root #Your database username
DB_PASSWORD=#Your database password

 Migrations:

The first step in developing any web application is to design the database. Laravel offers a great way to design database schema and tables and able to migrate it easily over different systems known as 'Migrations'.

Migrations are used to create, modify as well as share the application’s database schema. They are used with Laravel’s schema builder to build database schema easily. There are many benefits to create migrations for your database. You can easily rebuild your database structure using migration files on production or any other system.

Don’t worry if you find this explanation confusing. Believe me, you will get your way soon. Just follow along.

Execute the below command:

php artisan make:migration create_tbl_test_table --create=tbl_test
public function up()
{
    Schema::create('tasks', function (Blueprint $table) {
	  $table->bigIncrements('id');
	  $table->string('description');
	  $table->integer('user_id')->unsigned()->index();
	  $table->timestamps();
    });
}

The new column named “description” will store the task description and the column named “user_id” will store the id of the user who created the task. We added “ ->unsigned()->index()” after the user_if because it is a foreign key from users table.

Now We finished creating the database schema. To use this schema to create tables in the database, execute the below command. The migrate command will update the changes made in the schema to the actual database.

php artisan migrate

Controllers

Controllers are used to directing traffic between views and models and they can group multiple request handling logic into a single class. Thus, generally, they receive all the requests,s and based on their logic, they redirect or return respective data. In our example, now we are moving towards the front-end, thus we should make controllers through which we can handle requests coming to our application. Execute the below command to create a controller for tasks:

php artisan mark:controller TestController

This will create TasksController and you can find that in the app/Http/Controllers directory.

 Routing

Routing means accepting the request and redirect it to the appropriate function. Our app needs five routes which will do the following:

  1. Login
  2. Register
  3. Display list of all our tasks
  4. Add new tasks
  5. Delete existing tasks

Login and register are added by Laravel Jetstream so now we need to take care of only three routes.

Laravel provides various route files inside the '/routes' folder for various use cases. For example, the routing configuration for API will go in the '/routes/api.php' file while the routing configuration for our regular web application will go in the '/routes/web.php'.

Now, let’s edit web.php. Below is the edited version of the file. Make changes accordingly:

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TasksController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('welcome');
});
Route::get('home', 'App\Http\Controllers\Auth\TestController@view')->name('home');

Here, we have done two changes:

  1. We have grouped all routes so we can apply auth: sanctum and verified middleware to all routes thereby restricting those pages to only verified, logged in users.
  2. We have modified the route for the dashboard, which will now pass the request to TaskController's Index function. And created routes for other actions.

 Views – Blade Templates

Views are stored in the resources/views directory. Views are the front-end of the Laravel application and it separates application logic and the presentation logic. We need to create and design the following views:

  1. dashboard.blade.php ( Dashboard will show a list of tasks)
  2. add.blade.php (Form which lets you add new task)
  3. edit.blade.php (Form which lets you edit any task)

Laravel comes with a decent layout that contains a navbar called app.blade.php located in the Views/layouts directory.

With the help of Laravel’s blade engine, you can divide your pages into sub-sections and also can use Laravel’s default navbar section for your new views.

Now, in the/resources/views folder, create add.blade.php and edit.blade.php files with the markup given below.

Conclusion:

Over this long Laravel tutorial, we’ve learned how to install Laravel, configure the database, basic concepts like routes, models, views, and controllers by building your first Laravel application – todo Laravel application.

Hope so this Laravel tutorial helped you to understand Laravel’s basic concepts as well as motivated you to learn further. Please comment on your views about Laravel as well as about this tutorial and also you can comment with your concerns and issues. I will be glad to help you. Thanks

Reactions

Post a Comment

0 Comments

close