Author: aynsoft123

  • Make Contact Form in Laravel

    Here are some steps to create contact form:

    Step1

    To make contact form in laravel, first open terminal and install laravel project using composer command:
    composer create-project –prefer-dist laravel/laravel Project

    Step2

    After installation of laravel, go to project directory and create a controller for handling http request:
    php artisan make:controller ControllerName
    For example:

    This command has make SendEmailController under app folder directory: “app/Http/Controller”

    After open “SendEmailController.php file”, we have make index function which is root function of this controller and under this function we have write return statement with view method and under this method we have write view file(“send_email”).

    Step3

    After write a method of view file, create a view file under resources folder and under view folder.

    Under this send_email.blade.php file we have made one form for send an email to controller and under this file we have “csrf_field” function for prevent the application from cross side request forgery attack. In this file also we have create the GUI such as text box for store details of name, create text box for store email and create the text area for store the message.
    Under this file we have write the some block of code for creating gui, and validating condition and write code to show success message under condition if form is successfully submitted.

    Step4

    After writing GUI and validation message in the view file, go to SendEmailController file and create method for validating data against form data.

    Under this controller class we have defined the Mail class and create mail statement, for this we can use laravel mailer class. After that we have write some block of code for validating the template class.

    Step5

    After validating data, we go to route folder and web.php file where we setup the route of both method SendEmailController and send_email.blade.php files.

    Under this file we write route class with get method with two arguments: “/sendemail” and “SendEmailController@index”, so when in browser we have type or paste the url “/sendemail” then it will call index method of SendEmailController index.

    And we will route class with post method for send method with two arguments: “/sendemail/send” and “SendEmailController@send”, so when in browser form is submitted and send request to send method then it will execute send method of send email controller.

    Step6

    Now we have proceeded for send email, so we have go to terminal (command prompt)and first stop laravel server and type command: “php artisan make:mail SendMail”

    This command will make Mailable class with name like SendMail under this mail folder in directory “app/Mail/SendMail.php”. In build function we have make dynamic subject which show the name of user who fill the form. And we have to create “dynamic.blade.php” file under the view folder. Under this dynamic php file the email body has taken.

    After the completion of SendMail file we set the variable under the dynamic.blade.php file we store the user name data and show the message query.

    Step 7

    After completed the all php file, now we have set the configuration body for send email from laravel application. For this we have go to “.env” file and here mail driver option for send email. In this env file first we have to set username and password. The user name and password we get from the https://mailtrap.io/.

    To getting the mail username and password we have create an id and after the id created then we have go to the login option. After the login we can see the user name and password:

    Results

    After the completion of all file we have start your server, using server artisan command: “php artisan serve”.
    After run this command this will give you an address of server.

    Then go to your browser and type address “http://127.0.0.1:8000/sendemail”.

    To check validation:

    Successfully submitting form

    Check sender’s mail: Go to you mailtrap.io and check into your mailbox list.

  • Laravel Homestead

    Laravel Homestead provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. Laravel Homestead is an official, pre-packaged Vagrant box. Vagrant is a tool for building and managing virtual machine environments in a single workflow and provides a simple, elegant way to manage and provision Virtual machine.

    Homestead runs on any Windows, Mac or Linux system and includes:

    • Naginx
    • PHP
    • PostgreSQL
    • Redis
    • Memcached
    • Node
    • MariaDB
    • Mailhog
    • Beanstalkd
    • Git

    And include all other software you need to develop amazing Laravel applications.

    Installation of Homestead:

    1. Download And Install The Software

    Before running the Homestead environment in your machine, first, you must install VirtualBox6.x, VMware, Parallels or Hyper-v. You must download and install any of this software. So in this article, I have installed the VirtualBox6.x. You can download VirtualBox6.x from this link: https://www.virtualbox.org/wiki/Downloads.

    After installation of VirtualBox6.x or any of this software, you must install Vagrant. You can download vagrant from link: https://www.vagrantup.com/downloads.html

    2. Installing Homestead:

    Once VirtualBox6.x and Vagrant have been installed, you should add “laravel/homestead” box to your Vagrant installation using the following command in your terminal:

    i. Run this command: “vagrant box add laravel/homestead”
    Running this command in your terminal, it will take few times to install homestead in your vagrant box which depends on your internet connection speed.

    ii. Run this command: git clone https://github.com/laravel/homestead.git ~/Homestead.
    After installing Homestead in vagrant box, then you should have install Homestead by cloning the repository onto your machine

    iii. Run this command: “cd ~/Homestead”

    And then type the command “bash init.sh”

    This “bash init.sh” command will create the Homestead.yaml configuration file.
    IV. Creating SSH key and run the command. After run this command, simply press the enter button whatever command line asks you.
    ssh-keygen –t rsa –C “your_mail@gmail.com”

    3. Configuring Homestead:

    Next, you should edit the “Homestead.yaml” file included in the repository. In this file, you can configure the path to your public SSH key, as well as the folders you wish to be shared between your main machine and the Homestead virtual machine.

    Let’s edit our “Homestead.yaml” configuration file which we generated. Directory of this file is located at: “C:\Users\USER_NAME\Homested”.

    The folders property of the Homestead.yaml file lists all of the folders you wish to share with your Homestead environment.
    And the sites property of the Homestead.yaml file allows you to easily map a “domain” to a folder on your Homestead environment.

    After configuring the “Homestead.yaml” file, you have to edit the “hosts” file, which is located at directory “C:\Windows\System32\drivers\etc”. And add the address (192.168.10.10 homestead.test) at the end of your host file.

    4. Launching The Vagrant Box:

    Once you have edited the Homestead.yaml, run the “vagrant up” command from your Homestead directory. This will boot the virtual machine and automatically configure your shared folders and Nginx sites.

    After the “vagrant up” command, use the “vagrant ssh” command. This will login to you with your virtual machine.

    5. Creating Laravel Project:

    Let’s create our first Laravel project through homestead. Type the command “composer create-project –prefer-dist laravel/laravel test”. It will create new Laravel project in a folder name test.

    Installation of laravel project

    6. Running Our Application:

    Enter “homestead.test” from your browser and make sure homestead is running. You will see welcome page now.

  • Laravel Cron Job

    In this article, I’ll discuss an overview about Cron and then we’ll discuss, how to add or perform cron (Task scheduling) in Laravel.

    Why we need to use cron job?

    Because if you want to schedule your task like as, you want to send email automatically to users on daily morning or months or you want database log will be clear after week or months, therefore cron job will be needed to perform these scheduled tasks.

    What is Cron?

    Cron is basically a task scheduler, which perform the scheduled tasks at certain intervals. It typically automates system maintenance or administration though its general purpose nature makes it useful for things like downloading files from the interval and downloading email at regular intervals. For managing the schedule process cron uses a configuration file called crontab or cron table.

    How to add Cron Job in Laravel.

    Laravel command scheduler allows you to define your command schedule within Laravel itself. Basically your task schedule is defined in the “app/Console/Kernel.php” method. In this article, I will add a cron in Laravel and define a simple task to perform clear the data entries from the database table. Now, just follow these steps:

    Step1: Install Laravel

    Run the composer command: “composer create-project –prefer-dist laravel/laravel TaskSchedule”.

    Step2: Setup Database Entries:

    Run the artisan command: php artisan make:auth. This command will create authentication in your laravel project.

    Next, create database and link with you Laravel project:
    Create Database:

    Open .env file and add Database entries like this:

    run the migrate command: php artisan migrate. This will create tables in your database.

    After migrate command database table look like this:

    Next, open browser and navigate the browser “localhost:8000/register” add entries for users table in database like this:
    Register User’s in user table:

    Database user’s table look like this:

    Step3: Defining Schedules:

    Create Command
    In this step, we create our custom command. This will execute with task scheduling. So, runs the artisan command: “php artisan make:command everyMinute”

    Next, open the app/Console/Commands/everyMinute.php file and make some change in signature, description, and in handle() function like this:

    Register on Task Scheduler

    In this step, our task schedule is defined in the “app/Console/Kernel.php” file’s schedule method. So, open Kernel.php file and defined the task like this:

    Now, you can check your own custom command created or not. So, run the command “php artisan list”. You will result like this:

    Step4: Run Scheduler:

    Finally, run the scheduler command and test your cron job. “php artisan schedule:run”

    After running the scheduler, your data will be cleared from your database users table.

    #Schedule Frequency Options:

    There are a variety of schedules you may assign to your task:

    Method Description
    ->cron(‘* * * * *’); Run the task on a custom Cron schedule
    ->->everyMinute(); Run the task every minute
    ->->everyFiveMinutes(); Run the task every five minutes
    ->->everyTenMinutes(); Run the task every ten minutes
    ->->everyFifteenMinutes(); Run the task every fifteen minutes
    ->->everyThirtyMinutes(); Run the task every thirty minutes
    ->->hourly(); Run the task every hour
    ->->hourlyAt(); Run the task every hour at 17 mins past the hour
    ->->daily(); Run the task every day at midnight
    ->->dailyAt(’13:00’); Run the task every day at 13:00
    ->->twiceDaily(1, 13); Run the task daily at 1:00 & 13:00
    ->->weekly(); Run the task every week
    ->->weeklyOn(1, ‘8:00’); Run the task every week on Monday at 8:00
    ->->monthly(); Run the task every month
    ->->monthlyOn(4, ’15:00’); Run the task every month on the 4th at 15:00
    ->->quarterly(); Run the task every quarter
    ->->yearly(); Run the task every year
    ->->timezone(‘Asia/Kolkata’); Set the timezone
  • What is Composer

    Composer is an application level package manager for the PHP language that provides a standard format for managing dependencies of PHP software and libraries. In other words we can say that, “it is a tool for dependency management in PHP”, which deals with “packages” and “libraries”.

    Composer runs on “command line” and allows you to declare the libraries your projects depends on and it will manage (install/update) them for you. But it manages them on a per-project basis and installing them in a directory inside your project.

    Note: Composer is strongly inspired by Node.js’s “npm” and Ruby’s “bundler”.

    Composer allows users to install PHP application that are available on “Packagist” which is its main repository containing available packages. Link: https://packagist.org.

    What does composer do?

    Suppose:

    • You have project that depends on a number of libraries.
    • Some of those libraries depend on other libraries.

    Composer:

    • It enables you to declare the libraries you depend on.
    • Finds out which version or packages need to be installed, and it downloads them into your project.

    Basic Commands

    Composer offers several commands parameters including:
    1) require: this command add the library in parameter to the file “composer.json” and install it.
    2) install: this command install all libraries from “composer.json”, and use to download all PHP repository dependencies.
    3) update: update all libraries form “composer.json”.
    4) remove: uninstall a library and remove it from “composer.json”.

    Supported Framework

    • Laravel (version 2 and later)
    • Symfony (version 4 and later)
    • CodeIgniter (version 3.0 and later)
    • CakePHP (version 3.0 and later)
    • FuelPHP (version 2.0 and later)
    • Drupal (version 8 and later)
    • TYPO3 (version 6.2 and later)
    • SilverStripe (version 3.0 and later)
    • Magento (version 2.0 and later)
    • Yii (version 1.1 and later)
    • Zend Framework (version 1 and later)
    • Silex (web framework)
    • Lumen (web framework)

    System Requirements:

    • Composer requires PHP 5.3.2 or latest version to run on your machine.
    • Composer is multi-platform and it run equally well on Linux, Windows, and macOS.
    • A few PHP sensitive php settings and compile flags are also required, but when using installer you will be warned about any incompatibilities.

    How to use it with Laravel?

    Basically, to work in the Laravel framework you have to install the composer first. Without composer Laravel framework does not work. So, before using Laravel, make sure you have composer install on your machine.

    Installing Composer

    Installing Composer is easy. You can get information about installing composer at https://getcomposer.org/.

    After download and run composer-setup.exe. It will install the latest Composer version and set up your PATH so that you can call “composer” from any directory in your command line.

    Fig (a): Options in composer

    In Laravel, composer commands are used for creating the project, as well as used for update the laravel version, clear cache, upgrade and for other work as per requirement.

    Fig (b): Available commands in Composer

    For creating the Laravel project, type the command in your terminal “composer create-project –prefer-dist laravel/laravel projectname”. Without Composer command, Laravel project will not create.

    For example

  • How to create an Admin-Panel in Laravel

    In this, article we will discuss the creating an Admin Panel. As we know admin panel is one of the most important modules for your web or mobile application. In admin panel there is no role of application End users, only an Admin has the role to manage the application.

    In this article, we have customized the admin panel and add some features such as: add new users, delete update users, login and signup page. If an Admin Login to an Admin Panel and do some activities like CRUD (create, read, update and delete) operations then activity should be created and should be available for Superuser to see.

    How to create an Admin-Panel in Laravel?

    1. Create Laravel Project

    First you have to create a Laravel project using composer command:
    “Composer create-project –prefer-dist laravel/laravel project_name”

    2. Choose Performance Over Personalization

    In this user have two options for designing user interface or download the free available theme for an admin panel. So many of designers download the theme and customize the theme as per user’s requirements. Downloading template will help to save time for creating the user interface.
    So, in this article, I have downloaded the template and use their css, and java scripts file and customize the pages.

    Steps for applying template in Laravel
    2.1. First you have to download the template and after that copy the folder like css, js, scss, and image or assests folder. And go to your Laravel project directory and paste these assests folder in public directory of your laravel project.

    2.2. Next, after copying the assets under the public directory, then we will create a master file under the “resources/view/layouts” directory. In this “master.blade.php” we have added and edit the template file code which has a user interface for the admin-panel. Then we will create a “dashboard.blade.php” file under the “resources/views/admin” folder, you can manually create the layouts and admin folder under these resources. This “dashboard.blade.php” file is used we have used for routing the dashboard of Admin panel.

    master.blade.php file

    dashboard.blade.php file

    3. Database Connectivity

    3.1. In this article, I have used the Wamp server for database. Go to the phpMyAdmin option in Wamp and create database look like:

    3.2. Next, open “.env” and edit database connection look like:

    4. Create Login Interface Using Authentication:

    Laravel provide the authentication command for creating the login interface. Here we discussed how to setup an authentication for your project.
    4.1. First run the artisan command in your terminal under your laravel project:
    “php artisan make:auth”.

    This command will create the authentication folder under “views/auth” directory.

    4.2. After auth command, go to “database/migration/” and open file “create_user_table.php”. Under this file we have add two field “phone” and “usertype” field for admin panel.

    4.3. After that, open file “register.blade.php” under the “resources/views/auth” and write code for adding the phone label field.

    4.4. Next, we will modify the “User.php” file (“app/user.php”) and “RegiserController.php” file located at “app/http/Controller/auth/RegisterController.php”.

    User.php

    RegisterController.php

    4.5. After modifying the files, run the migrate command:
    “php artisan migrate”

    After running this command, tables are created automatically in your database look like:

    4.6. After migration, navigate the URL: http://127.0.0.1:8000 into your browser, and register the user.

    Register User

    Manually define usertype “admin” into your database
    Then go to for middleware to redirect this admin into your dashboard.

    5. Creating Middleware:

    After completing the setup of dashboard, database and authentication setup add middleware for redirect to the admin dashboard.
    5.1. Run the middleware command: “php artisan make:middleware middleware_name”

    Next, go to directory “app/http/middleware/” and open “AdminMiddleware” file and write code look like:

    5.2. Next, updated the “kernel.php” file under the “app/http/” directory:

    5.3. Next, open the “LoginController.php” file under the “app/http/controller/auth” directory and add class and function like:

    5.4. After the completion of middleware, route the dashboard using middleware in the Laravel. Open “web.php” file under the “routes” directory.

    6. Login In Admin Panel

    6.1. Open browser navigate the URL: http://127.0.0.1:8000/dashboard.

    6.2. After navigating the URL, The login page will redirect after that enter the admin credentials:

    6.3 After entering admin credentials, Dashboard page will open:

    6.4. Logout

    6.5. Register User’s

    6.6. User’s Profile

    6.7. Edit User’s by Admin

    6.8. Delete User

    Note: you can download the code form GitHub directory, link: https://github.com/digitalcrm/Admin-Dashboard.

  • Admin Panel in Laravel

    In this article we will discuss the how to create admin panel in Laravel. So, creating an admin panel in Laravel we have use the Laravel package “Voyager- The Missing Laravel Admin”.

    So, the first question arises is why we using the “Laravel Voyager” package. Basically “Voyager” is a powerful package to handle the complex operations such as handling the CRUD (create, read, update and delete) operations and also the optimization of the server side and client side code.

    Voyager has the BREAD system, which allows you to browse, read, edit, add and delete any content on your site. Voyager is simply an admin for your application, you have full control over how your Laravel application will function on the front-end and then you can pass off the entire administrative task to Voyager.

    Features provided by Voyager package:

    • Module Management
    • Roles and Permissions Management
    • Routing
    • Media Manager
    • Menu Builder
    • BREAD Builder
    • Compass
    • The Database Manager
    • Multi Language Support

    Note: To know more about “Voyager” visits this link: https://voyager.devdojo.com/.

    Installation or Creating Admin Panel

    1.First you have to create a “Laravel” project inside your project directory. Run this command using the composer: “composer create-project –prefer-dist laravel/laravel projectname”.

    2. Once the project installs, you can include the “Voyager package” with command: “composer require tcg/voyager”. Run this command in your terminal inside your Laravel project directory.

    3. After running the voyager command, create a new database.

    4. After creating a new database, then go to your project and modify database driver in the “.env” file and also modify the “APP_URL”.

    5. Next, open your project, go to directory “config/app.php” file and add the Voyager Service provider in the provider array.
    “TCG\Voyager\VoyagerServiceProvider::class,”
    “Intervention\Image\ImageServiceProviderLaravel5::class,”

    6. Finally, you can install Voyager. There are two options to create admin panel with a dummy data or without dummy data. Commands are:
    With dummy data: “php artisan voyager:install –with-dummy”.
    The dummy data will include 1admin account (if no users exist), 1 demo page, 4 demo posts, 2 categories and 7 settings. This will contain the default login credentials:
    Email: admin@admin.com
    Password: password
    OR
    Without dummy data: “php artisan voyager:install”.
    In this article, I have installed the Voyager using dummy data.

    7. After successful installation navigate to “http://localhost/admin/”.

    After browse the address, you will be asked to enter the user name and password.

    After Login, the dashboard homepage will look like this:

    User’s panel

    Add New user in Users panel

    adding users, it will look like

    Note: In this panel, you can customize the dashboard as you wish to want and you can add more features. The directory of this panel is located at: “vendor/tcg/voyager”.

  • Installation of Laravel in Windows

    Here are some steps required to install Laravel in Windows operating system

    1. Server Requirements

    Installing Laravel first your system must satisfied few system requirements

    • PHP >= 7.13
    • BCMath PHP Extension
    • Ctype PHP Extension
    • JSON PHP Extension
    • Mbstring PHP Extension
    • OpenSSL PHP Extension
    • PDO PHP Extension
    • Tokenizer PHP Extension
    • XML PHP Extension

    Laravel Homestead virtual machine provides the all server requirements needed to install Laravel. Before launching Laravel Homestead you must install VirtualBox 6.x, VMWare, Parallels or Hyper-V. After installation of these one you must install “Vagrant”.
    OR
    If you are not using Homestead then you will need to install other web server such as “WAMP” or “XAMPP” free web server. These web servers automatically install everything you need to develop Web applications and provide the server for your Laravel application.
    In this article, I have used the WAMP server.

    1.1. Install server using WAMP(Window, Apache, MySQL, PHP) server
    Download the web server from link: http://www.wampserver.com

    After completion of download, install and run exe file

    2. Installing Laravel

    After installation of web server, install the “Composer” first in your machine before installing Laravel.

    2.1. Download the composer from the link: https://getcomposer.org/download/

    Download and run the composer-setup.exe file

    Installing composer
    For installing the latest version of Laravel project, make sure that you have selected the latest PHP version.

    2.2. Installing Laravel Via Composer
    Open terminal (cmd) in your machine and use directory “wamp64\www”

    After open the terminal check the composer is installed or not by using “composer” command’


    Install Laravel framework by issuing the composer “create-project” command in your terminal

    composer create-project –prefer-dist laravel/laravel projectname
    This command will create the latest version of Laravel under the “wamp64/www/” directory.

    After the project installed in your directory and we have installed php server, you may use “serve” artisan command to start development server: http://127.0.0.1:8000
    Command: php artisan serve

    Note: After installation of php server and composer, you must add environment variable on your system before start your project. These directories exist in different location based on your operating system, but some common locations of directory in windows are:
    Local server: “C:\wamp64\bin”
    Composer: “%USERPROFILE%\AppData\Roaming\Composer\vendor\bin”

  • Laravel Authentication

    In comparison to other framework, Laravel makes authentication very simple and almost everything is configured for you out of the box. You have to configure it. The authentication configuration file is located at “config/auth.php”, which contain several well documented options for tweaking the behaviour of the authentication services.

    At its core, Laravel authentication facilities are made up of “guards” and “providers”. Guards define how users are authenticated for each request, and Providers define how users are retrieved from your persistent storage.
    In Laravel, by default, it’s includes a “user.php” Eloquent model in your “app” directory (“app/user.php”). This model may be used with the default Eloquent authentication driver. If your application is not using the eloquent model, you may use the database authentication driver which uses the Laravel query builder.

    When building the database for the app\user model, make sure the password column is at least 60 characters in length and string column length should be 255 characters. And also, you should verify that user table contains a nullable, string “remember_token” (used for store a token and ‘remember me’ option for users when logging into your application) column of 100 characters.

    How to Start Authentication

    Laravel provide several pre-built authentication controller, such as:

    • ForgotPasswordController.php
    • LoginController.php
    • RegisterController.php
    • ResetPasswordController.php
    • VerificationController.php

    1. Setup Authentication

    First open your terminal and run the command:
    “php artisan make:auth”. But keep in mind, this command should be run on fresh laravel application.

    After run this command, this will install a layout view, auth view, as well as routes for all authentication end-points. These files are located in “resources\views” directory.

    Now, we have all views files and routes files, your application look like:
    Home Page

    Login Page

    Register Page

    2. Creating Database:

    After setup the authentication, then create the database into your local server: in this, I have use “Wamp” server for Laravel project, with database LDb name:

    After creating database, then go to your project and open “.env” file and do some changes: such as DB_DATABASE, DB_USERNAME and DB_PASSWORD.

    3. Run Migration

    Migration are like version control for your database which allowing you to easily modify and share the application’s database schema. Migrations are typically paired with Laravel’s schema builder to easily build your application’s database schema.

    In this project, I have generated only database and no schema is created. After run the migration, it will automatically create the table in your database.
    Run command: “php artisan migration”.

    After run migration command, three tables will be automatically generated in your database such as migration, password reset and users.

    Users’ field

    Password_reset

    Migration field
    After completion of all process, your project will work properly with authentication process.
    Note: In your “resources\views” directory, all of these views use the Bootstrap, CSS framework, but you are free to customize them however you wish.

    Results

    Register Page

    Login Page

    If you enter wrong password

  • How to make a Blog in Laravel

    In this article, we will go through how to make a blog in Laravel. We will build a simple blog and admin panel for managing the users and task. Source code for this project is available on GitHub, link: https://github.com/digitalcrm/Blog-Laravel.

    1. Installation

    First you will need a fresh installation of the Laravel framework. Install the Laravel framework using composer:
    “composer create-project –prefer-dist projectname”

    2. Prepping the Database

    2.1.Connect Database with your Laravel project:

    Go to your database server and create database. In this article, I have used the wamp server phpMyAdmin :

    Connect with your project, go to “.env” file and change the DB_CONNECTION, Database, User_name and password. In phpMyAdmin the default value of user_name is root and password is null(empty):

    3. Make Database Model and Migrations

    Next, create migrations and models for your blog. For creating model/migration run the artisan command in your terminal: “php artisan make:model model_name –m”. Here “-m” refers to migrations.

    In this article, I have created different models and migrations for User and Admin. The migration will be place in the “database/migrations” and Model will be placed in “app/model” directory of your project.

    Model “post.php”

    Migration table “create_posts_table.php”

    After creating and setup the migrations accordingly to your blog databases, run the migrate command in your terminals. Command: “php artisan migrate”

    After running migrate command, tables will be generated in database:

    4. Routing

    Next, we will add routes to our blog or application. Basically routes are used to points URLs to controllers or closure function that should be executed when a user accesses a given page. By default, all Laravel routes are defined in the “routes/web.php” file.
    In this article, I have created many routes for User, Admin, Home and Auth.

    5. Controller

    After adding the routes, we will need a controller for our blog or project. In Laravel all controller are generated in the “app\Http\Controller” directory. Command:
    php artisan make:controller ControllerName –resources
    OR
    php artisan make:controller ControllerName
    Resource Controller command

    Resource Controller (“app/Http/Controller/Admin/PostController.php”) file

    Controller Command

    Controller File (“app/Http/Controller/User/PostController.php”)

    In this article, I have created the different controller for the User, Admin and Home part of our blog.

    In the Admin controller, I have duplicated the auth controller for admin side authentication purpose.

    6. Authentication

    Run the auth artisan command in your terminal for creating the authentication in your project.
    Command: “php artisan make:auth”.

    After running this, the command will generate auth controllers, views and routes for your project.

    Controller: Auth controllers are generated in “app/Http/Controllers/Auth” directory.

    Views: auth views are generated under in “resources/views/auth” directory.

    7. Building Layouts & Views

    In this article has multiple views which contain a form for Admin, Blog, role, category, permission, tag, list users and as well as a listing of all blog. As we know all Laravel views are stored in “resources/views”. So we have defined multiple layout view in this directory.

    7.1. Layouts
    In this layout, we have created the app layouts for Login, Register and Logout.

    7.2. Admin Views
    In this view, we have created multiple views for admin task and also created an admin panel and blog page layouts.

    7.3. User Views
    Basically, as we know all web applications share the same layout across pages. So, in this view, we have defined the layouts for users. Laravel makes it easy to share these common features across every page using Blade layouts.

    8. Adding Tasks

    After creating the view, we have to move for create, update, store, edit, and delete tasks for each route of controller. Example for post-controller:

    create

    store

    edit and update

    destroy

    9. Authorization

    Laravel uses policies to organize authorization logic into simple, small classes. Typically, each policy corresponds to a model. In this article, we create a PostPolicy using the artisan command:
    “php artisan make:policy PostPolicy”

    After this command, we will add some validation in “post policy” as needed.

    10. Blog Interface

    Home Page

    Admin Dashboard

    Blog Create

    User’s Blogs

  • Laravel Notification

    Notifications are short informational messages that notify users of something that occurred in your application. Basically, Laravel provides support for sending notification across a variety of delivery channels, including mail, database, sms, markdown, broadcast and slack.

    Creating Notifications in Laravel

    In Laravel, each notification is represented by a single class and stored in the “app/Notifications” directory. This directory will be created for you when you run the “make:notification” Artisan command in your terminal.

    Basically Laravel feature provides the notification system; therefore, we just have to configure it on our Laravel project. In this article, we will show how to add notification system in Laravel. So, just follow these steps:

    Step1: Install New Laravel Project

    Create a new Laravel project with composer:
    composer create-project –prefer-dist laravel/laravel LaravelNotification

    Step2: Setup Database

    Next, setup the database and mail driver and add the database and mail driver credentials to “.env” file.
    Create Database:

    database credentials to .env file:

    Browse the mailtrap.io and create your own id, then you will get your own credentials like this.

    Add your Username and Password to .env file like this:

    Step3: Create user table and Authentication

    For creating the users table run the artisan command:
    “php artisan migrate”.

    Next, add authentication using auth command:
    “php artisan make:auth”

    Register the user in users table, navigate your browser localhost:8000/register

    Step4: Create Notification:

    Next, we need to add notification in your Laravel project. So run the artisan command:
    “php artisan make:notification SendNotification”

    After running the command, the file will be generated at app/Notification/SendNotification.php directory:

    Step5: Send Notification:

    Send Notification Using Notifiable Trait:
    Open routes/web.php file add “Notifiable Trait” like this:

    OR

    Send Notification Using The Notification Facade:
    Either you can send the notification by using the “Notification Facade” like this:

    Step6: Next, Navigate The Browser:

    After configuring the notification, navigate the URL: 127.0.0.1:8000/ or localhost:8000 and then check the demo inbox in mailtrap.io you will get the notification like this: