Categories
Laravel

Laravel API POST methods request return a 419 status code when we browser by dev server.

If the browser cannot access the Laravel API post routes at localhost, it will always return an HTTP 419 error status, with the CSRF token mismatch exception. We need to changes in the Laravel app/Http/Middleware/VerifyCsrfToken.php file.

This code is located in the handle method of the parent class of VerifyCsrfToken.php, which is found at vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php.

Solution: Simply add the isReading() method to the file verifyCsrfToken.php to enable the POST method.

Categories
Google API Laravel

How can we setup the Google APIs Client Library in Laravel 10?

Setting up the Google APIs Client Library and access using Service Account in Laravel involves several steps. This library allows you to integrate various Google APIs, such as Google Drive, Google Sheets, etc., into your Laravel application. Here’s a step-by-step guide to help you set it up:

Note: Before you begin, make sure you have Composer installed and your Laravel project is up and running.

Create a New Laravel Project (Optional):

Install Google API Client Library:

Use Composer to install the Google APIs Client Library

Create Google API Project and Get Credentials:

Enable the Required API:

  • In the Cloud Console, navigate to the “APIs & Services” > “Library” section.
  • Search for the API you want to use (e.g., Google Sheets, Google Drive) and enable it for your project.

Create a Service Account:

  • In the Cloud Console, navigate to the “APIs & Services” > “Credentials” section.
  • Click on “Create credentials” and select “Service account key.”
  • Follow the prompts to create a new service account. Give it a name and assign it the necessary permissions based on the API you’re using (e.g., Editor or Viewer access).
  • Download the JSON key file for the service account. This file will be used in your Laravel project.

More details google developers console to generate service-account credentials.

Upload your service account json file to “storeage/app/public/service-account.json” directory.

Configure Laravel Environment:

Open your Laravel project’s .env file and add the following lines, replacing the placeholders with your actual credentials:

Assign google Service Account config:

Create Routes and Controllers:
Test Your Application:

Access the application in your browser and navigate to the http://sitedomain.com/drive/all/files route to get list of all files and folder.

Categories
Laravel

Laravel – Failed to authenticate on SMTP server with username using 3 possible authenticators

If you still get following error (Failed to authenticate on SMTP server with username) when sending email please follow below steps.

Swift_TransportException

Failed to authenticate on SMTP server with username “yourid@gmail.com” using 3 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code “535”, with message “535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials c17-20020a170902d49100b001624cd63bbbsm4334145plg.133 – gsmtp “. Authenticator PLAIN returned Expected response code 235 but got code “535”, with message “535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials c17-20020a170902d49100b001624cd63bbbsm4334145plg.133 – gsmtp “. Authenticator XOAUTH2 returned Expected response code 250 but got code “535”, with message “535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials c17-20020a170902d49100b001624cd63bbbsm4334145plg.133 – gsmtp “.

Step 1:

Please login into your gmail account and click on “Manage your Google Account” button form right to your profile picture.

Manage your Google Account

Step 2:

Go to to Security tab form left menu and please turn on 2-Step Verification by using your Recovery Phone.

2-Step Verification

Step 3:

Click on App Password bar form Sign in to Google section.

App Password

Step 4:

Select the app and device you want to generate the app password for. And enter a platform name and click on GENERATE button to generate new password.

Generated password

Finally copy your new generated password and save it your self also use it to your other device or app where you want to send SMTP email.

Step 5:

Now change password on Laravel .env file MAIL_PASSWORD value with new generated password.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Hope it will help you.

Categories
Laravel

Laravel – Send email from any controller action

Laravel send email from any controllers. In this article, I am going to discuss the Mail API in the Laravel web framework. Laravel provides a clean, simple API over the popular SwiftMailer library with drivers for SMTP, Mailgun, sendmail etc. you are allowing to quickly get started sending mail.

The below are some easy steps to follow, and you will be able to send email in Laravel controller.

  • Install Laravel.
  • Create/Update Controller
  • Define a Controller Route.
  • SMTP configure in the .env file.
  • Create a Mailable class.
  • Create a Email Template.

Install Laravel.

Run following command to create new Laravel project.

Create/Update controller

Create new controller or update already defined method in your existing controller. Here I have create a new controller using below command.

After successfully create new controller you can update below code in your file.
app\Http\Controllers\EmailSendController.php

Define a route to send an email.

Now create a route in your web.php file to sending email to the user which you want.

Setting up SMTP details in .env file

We will use gmail SMTP to configure the settings in the .env file.

Create a Mailable class

Open your terminal and go to project root directory and type the following command.

So, it will create a file inside App\Mail\ContactUsMail.php. Now, this class catch form submitted data, and pass data to email template.

Create a email Template file in resources/views/emails/contact-us.blade.php

Now, you need to create a blade file inside the views/emails folder where you show email content and send form in laravel controller.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please SHARE this article

Categories
Laravel Vue Js

Laravel 8.x configure Vue JS CRUD with Vue Router, Vuex, Vue-Axios

In this post I am going to showing example how to configure Vue JS CRUD with Laravel 8.x. We will create a Single Page Application(SPA) using the Laravel 8.x and Vue JS. In this post you will learn how to use vue router and v-form with vue-axios package how to work with laravel and vue js crud. I am showing example on Windows 10 with XAMPP server. More details on my previous post Install Laravel on xampp server.

First Install Composer on your computer, click here to download and install composer. Also please install git bash, click here to download and install git bash. Now we use git bash to run command.

Download the NodeJs pre-built installer for Windows, click here to download and install NodeJs on your computer.

Install Laravel 8.x in xampp directory “laravelvue”:

Go to your E:\xampp\htdocs directory, right click open git bash terminal and run command composer create-project laravel/laravel laravelvue to install Laravel.

Got to “laravelvue” directory or your project directory by command cd laravelvue.

Install Laravel UI & Auth:

Now Install Laravel UI package by command composer require laravel/ui, Once the laravel/ui package has been installed, you may install the frontend scaffolding using the ui Artisan command php artisan ui vue –auth

Now create controller with Invokable by Artisan command make:controller, That command is fired into the app/http/controllers directory, go to controllers directory and you can find a new PHP file PagesController.php

Go directory app/http/controllers and open file PagesController.php and welcome view in __invoke method.

Now we have to create resources/views/welcome.blade.php vie blade file look like following code.

Now add following route in routes/web.php

So now it’s Vue time:

Now Install NPM by command npm install && npm run dev

Now Install VUE by command npm install vue

Now install all required VUE tools and libraries by a single command:

OR

Now Install vue-router by command npm install vue-router, or for details click here

Now Install vuex by command npm install vuex –save, or for details click here

Now Install axios and v-form for client side validation by command npm install –save axios vue-axios vform, or for details click here

Now make ready resources app js in directory resources/js/app.js

Now make routes libraries js in directory resources/js/routes.js, with related component.

Now run following command to compiled js and css resource.

Now create component files in directory resources/js/components/*.vue files, with related component.

  • AppComponent.vue
  • HeaderComponent.vue
  • FooterComponent.vue
  • LoginComponent.vue
  • RegisterComponent.vue

resources/js/components/AppComponent.vue

resources/js/components/HeaderComponent.vue

resources/js/components/FooterComponent.vue

resources/js/components/LoginComponent.vue

resources/js/components/RegisterComponent.vue

Now every thing setup is done. Now you are ready to check it.

configure-vue-js-view

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Hope it will help you.

Categories
Laravel

Laravel 8 Target class [TableSeeder] does not exist

Today I am getting this error when I am trying to refresh seed with php artisan migrate:refresh –seed command. Database Seeder class is called another seeder class with $this->call(CmsPagesSeeder::class); But geeting error Target class [CmsPagesSeeder] does not exist.

Finally I have apply follwing solution and now working fine with command php artisan migrate:refresh –seed or php artisan db:seed or php artisan db:seed –class=CmsPagesSeeder

Laravel 8 Seeders and Factories are now need to add namespace at the top of class file.

Example:

STEP: 1

Rename directory database/seeds to database/seeders

STEP: 2

Open composer.json file and change on autoload section.

STEP: 3

Open Database Seeder file database/seeders/DatabaseSeeder.php and add namespace Database\Seeders; on top of page.

STEP: 4

Open your others seeders file and add namespace Database\Seeders; to all seeders class.

STEP: 5
Done! Now run following commands you can see now DB seeder is working fine.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please LIKE and SHARE

Categories
Laravel

Laravel 7.x Generate Database Seeding

Laravel: Generate Seeders

Laravel includes with a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. We can set any name for Seed classes, but we should follow some sensible convention, such as ProductsTableDataSeeder, etc. By default, a DatabaseSeeder class is defined for you. Using default class, you may use the call method to run other seed classes, allowing you to control the seeding order.

Creating a new module

We can creating a simple module by run the following command to create a module. Replace MODULE_NAME by your desired name.

Also we can create multiple modules in one command like below.

How to Generate Seeders?

We can generate a seeder by the Artisan command make:seeder. All of generated seeders class will be placed in the database/seeds directory:

Calling default DatabaseSeeder with Additional Seeders

Default the DatabaseSeeder class, you may use the call method to execute additional seed classes. Using the call method allows you to break up your database seeding into multiple files so that no single seeder class becomes overwhelmingly large. Enter the name of the seeder classes you want to run:

Laravel: Running Seeders

Once you have done generate your seeder, you may need to regenerate Composers autoloader using the dump-autoload command.

To seed your database now you may use the Artisan command db:seed. By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please LIKE and SHARE

Categories
Laravel

Laravel 7.x Creating Table or Database Schema Migrations

Laravel: Database Migrations

Laravel database migrations are like version control for your database, allowing you to modify and share the application’s database schema. Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel’s supported database systems.

How to create a migration?

We can create database migration by using the Artisan command make:migration New migration file be placed in project database/migrations directory. And each migration file name contains with timestamp, which allows Laravel to determine the order of the migrations.

Migration File Structure and Creating Table

Laravel migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method. To create a new database table by using Schema::create method.

Run all Migrations

Please execute the migrate Artisan command to run all of your created migrations:

Roll Back or Refresh Migrate Using Artisan Command

Artisan migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please LIKE and SHARE

Categories
Laravel

Laravel – Custom contact form and send email

Laravel custom contact form and send email are another routine functionality that is a requirement of more or less every website. In this article, I am going to demonstrate how you can easily create a contact form and mail function in Laravel with email sending.

Laravel is well known PHP Framework, providing multiple solutions for a problem. This is one of the main reasons of the popularity of the PHP framework. Most popular solutions cover this framework routine functionality such as authentication, sessions, routing, send email and caching.

Creating custom contact form in Laravel

  • Configure Laravel Project
  • Install HTML and Form Package
  • Configure Project Database
  • Create Database Table Using Migrations
  • Create ContactUs Model
  • Create Page Route contact
  • Create ContactUs Controller
  • Create contact Page View
  • Sending Email Function

Configure Laravel Project

Install and configure your Laravel project by using the Composer create-project command in your terminal: composer create-project laravel/laravel laraveldemo –prefer-dist “laraveldemo” is your project directory.

Install HTML and Form Package

To create custom contact form in Laravel, I will use laravelcollective/html package for contact us Forms and HTML. Launch the SSH terminal and login to your server using the Master Credentials and go to your project root directory. Run command composer require laravelcollective/html

After successful installed HTML and Forms package laravelcollective/html, goto config/app.php file and add Autoloaded Service Provider and Class Alias.

Configure Project Database

Now configure your project database connection, Find .env file in your project root directory, Open .env and add database access credentials there.

Create Database Table Using Migration Command

Go to your SSH terminal and Run artisan make:migration create_YOUR_TABLE_NAME_table command to create the database migration for creating the Contact Us model.

Contact us migration has been created, go to database/migration/2020_04_02_180504_create_contact_us_table.php and update up method public function up() with database fields like below.

Now run the artisan migrate command to generated database table contact_us with all (id, name, email, message, created_at, updated_at) fields.

Create ContactUs Model

Go to your SSH terminal and Run make:model MODEL_NAME command to create ContactUS model.

After successfully created the ContactUS model, go to App directory and Find file app/ContactUS.php and add the following code in the file.

Create Page Route contact

Now, We will go to create the route “contact-us” (Contact Us page URL). Go to routes/web.php and add the following code to the route:

Create ContactUs Controller

After setup page the routes, I will go to create a controller to handle the requests from the routes. Go to your SSH terminal and Run make:controller CONTROLLER_NAME command to create the controller.

Go to app/Http/Controllers directory and find and Open ContactUSController.php and add the following code.

Create contact Page View

Now I am going to create contact us Page View layout of the contact form. Go to resources/views/ and create a file contactus.blade.php. Place the following code in this blade (view) file.

Form output like below picture.

Sending Email Function

Custom contact us form is now functional. On submitting the form, the data will be stored in the database. I will now make the form email send function.

Now create another blade (view) file, resources/views/email.blade.php and add the following code in email blade.

Now go to project directory, Find .env file, Open .env and add SMTP email access credentials there.

Now SMTP email the configuration is done, update app/Http/Controllers/ContactUsController.php with the following code.

Laravel custom contact us form is now ready for deployment. This module save the data in the database and send email it to the given email address as well. To test the Contact Us form, go to the application and click on launch application (remember to add /contact-us to the URL). Thanks.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please LIKE and SHARE