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

Categories
Laravel

Laravel – PHP Artisan console commands with Laravel Framework 7.3

Introduction to Artisan

Artisan is the console command line interface included with Laravel Framework. Artisan provides a number of helpful commands that can assist you while you build your Laravel application.

How to use commands?

To view list of available Artisan commands by following command.

Output:

Each command also includes a “help” screen which displays and describes the command’s available arguments and options. We can view a help screen the name of the command with help

Available Aartisan console commands with description.

Below some most common uses commands with details:

clear-compiled

Every time when we run different commands on the Laravel application laravel framework generates automatic some cache files in bootstrap/cache folder. Those files are compiled.php and services.php. Both of them file keeps list of mostly used classes/services needed by your project configuration. This php artisan clear-compiled command removes all the files from cache folder.

down

To enable maintenance mode, we need to execute the down Artisan command: php artisan down Put the application into maintenance mode.

up

To disable maintenance mode, we need to execute the up Artisan command: php artisan up Bring the application out of maintenance mode.

serve

Create Local Development Server by artisan serve command php artisan serve The serve command is used to run the application using the PHP development server. This command is generally used for development and testing purposes.

migration

To generating migrations for database each table migration file name contains a timestamp which allows Laravel to determine the order of the migrations php artisan make:migration create_users_table migration file created on “database\migrations” directory. We can use the –path option when executing the make:migration command, If we would like to specify a custom output path for the generated migration.

db:seed

Laravel artisan db:seed command is used to add records to a database automatically using a Seeder Illuminate\Database\Seeder class to generate or provide the records. For the db:seed defines three options: class, database and force. By default the db:seed command will use for the DatabaseSeeder class, this class is defines in the project root database/seeds/DatabaseSeeder.php file. Importance seeder commands are below.

auth:clear-resets

The user auth:clear-resets command can be used to remove expired password reset tokens from the database.

cache

Flush the application cache.
Remove an item from the cache.
Create a migration for the cache database table.
Create a cache file for faster configuration loading.
Remove the configuration cache file.
Discover and cache the application’s events and listeners.
Clear all cached events and listeners.
Generate the missing events and listeners based on regist.

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

Categories
Laravel

Laravel install in localhost XAMPP server

Laravel install in localhost XAMPP server, please execute following 7 steps.

1. Download XAMPP for Windows, Linux or OSX Installer from here:
Download XAMPP and make ready your local web server.

2. Go to localhost web root directory E:\xampp\htdocs
Create a new folder for your project like “laraveldemo”

3. Run Command Prompt for Windows
Go to project root directory E:\xampp\htdocs

4. Download and run the Composer Windows Installer from here:
Download Composer

Download Composer
Download Composer

5. Check the installation using composer -V
Output: Composer version 1.10.1 2020-03-13 20:34:27

6. If Composer is works fine, than go to install laravel using the following command.
composer create-project laravel/laravel laraveldemo
OR
composer create-project laravel/laravel laraveldemo –prefer-dist “laraveldemo” is your project directory.

laravel composer create-project

7. After complete install and create laravel project we need to create Local Development Server by artisan command. Go to project root directory E:\xampp\htdocs\laraveldemo and run artisan command php artisan serve

php artisan serve command

Browser your laravel project using http://127.0.0.1:8000 url on browser and enjoy!

Laravel demo project

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