Laravel 5.5 Seeding Example | Laravel Seeding | Laravel Database: Seeding

Seeding is very useful while you wanted to keep some dummy data without manual interfere with Database. For example when you deploy you application you insert admin credentials into you users table every time you deploy so that admin user can login. You don't need to to insert those credentials manually every time.
Laravel provides feature of seeding and you can manage your dummy database in your seed classes.


You can create seed like below artisan command:

These command will create a new file 'database/seeds/ProductsTableSeeder.php' and 'database/seeds/UsersTableSeeder.php' which will contain a run() method. ...  Read More

Share This:


Laravel Migration Example | Database: Migrations - Laravel | Laravel 5.5 Migration Tutorial | Database Versioning

If we manage our database manually like if you need to add a column, You directly alter the MySQL database table and add the column.
If you need to add a new table into you MySQL database you directly create a new table.
But here is the question, If you are a number of developers and working on same project how will you get to know that who changed what in your database ?
If right now you are working on second or third version of app, How will you know what was your schema when you launched first version of your application? ...  Read More

Share This:


Second Highest Salary without subquery in MySQL | Nth highest Salary without subquery | MySQL second highest salary query

In so many interviews Interviewer will ask you a very simple MySQL query. And the question is :

How to select 2nd,3rd or the Nth highest salary from a table in MySQL ?

If you will answer with sub query like below query:

Next question might be asked like:

How to select 2nd,3rd or the Nth highest salary from a table in MySQL without using sub query ?

To get the second highest salary of an employee here is the MySQL query:

If you wanted to get 3rd highest salary then here is below MySQL query to get the 3rd highest salary:

If you wanted to get nth highest salary then here is below MySQL query to get the nth highest salary:

for example if you wanted to write MySQL query to get 4th highest salary of the employee then the query will be like:

  ...  Read More

Share This:


Syntax error or access violation: 1055 isn't in GROUP BY in Laravel | Laravel : Syntax error or access violation: 1055 Error

This error is not because of the Laravel actually. This is MySQL error and which comes if you have ONLY_FULL_GROUP_BY inside the SQL_MODE variable in your DB.

Of course you can handle it at MySQL level by removing ONLY_FULL_GROUP_BY from you SQL_MODE.

But Laravel also provides the functionality to handle this error.

check your config/database.php And check if inside the mysql settings there is one that is like:

If you turn 'strict' to false will resolve you issue like;

 ...  Read More

Share This:


Import csv in PHP | How to import csv file in PHP? | How to Import CSV File Data into MySQL Database using PHP | Import a CSV File Using PHP and MySQL | How To Import CSV File Into MySQL Using PHP? | How to Import CSV Into MySQL Database Using PHP | Import CSV data into MYSQL using PHP

import-csv-php-mysql

Import CSV in PHP & MySQL example

In most of the PHP web applications we stores data into MySQL database. We also export data in PHP or import data in PHP.

Here in this article I'm explaining how you can achieve the task of import CSV (comma-separated values)  into MySQL database with PHP.

In most of the projects uploading data or reports is a common task which we do so this article is very important for you to learn storing the CSV file data into MySQL.

A CSV file stores data in tabular format that can be separated by either comma(,) or semicolon(;). So this is very easy to read every row and insert all the rows into MySQL database. ...  Read More

Share This:


How do I get HTTP Request body content in Laravel | How to return JSON Request body in Response in Laravel | How to add new parameter in HTTP request body in Laravel

So many time we need to return in REST APIs the request body. For example if you send a POST request with some parameters and you wanted you API access token along with the request body parameters which you sent.

Like below example:

Here is the Request body:

And now you wanted to get register and access token in response along with above request body like below:

How can we do this in Laravel????

Here is the answer of this question. You can get the HTTP request body in Laravel by following code:

Now if you wanted to send the request body with along with access token in JSON response in Laravel, You can do it like following example:

So finally function to return the JSON response after registration of the user looks like:

  ...  Read More

Share This:


Customize validation error response in Laravel 5.5 | How to return Custom validation error response in JSON in Laravel 5.5 | How i can return a customized response in a FormRequest class in Laravel 5.5? | How to write validation rules for JSON request in Laravel 5.5

Laravel 5.5.* has made a change which is big deal with Laravel developers now a days and the change is you can't customize your validation error response like you you did in earlier versions. In earlier versions of Laravel we were able to customize or change our validation error response with below function example:

Now in current versions of Laravel 5.5.* you will not be able to customize your validation error response with this function.

You will require to use followings as well:

If you are using FormRequests to validate data and want to return custom JSON on error response in Laravel 5.5.*, You have to you below function.

It will return all the validation error in JSON. ...  Read More

Share This:


Export To Excel with Headers In PHP and MySQL | How to Export MySQL Data in Excel/CSV File using PHP/MySQL

export to excel from mysql in php

Export MySql data to Excel example

So many time we need to export data from MySql to Excel or CSV. So many time we need to send reports in excel and we require to export data from MySql to Excel.

We can perform this task very easily in PHP. Here in this article I'm going to explain how we can export the MySql data into excel or CSV in PHP.

I will be writing single PHP script which will work for excel and CSV both. We can do it by just changing the headers of file.


 ...  Read More

Share This:


Add Remove input fields dynamically using jQuery and PHP

add-more-field

Add/Remove field example in JQuery and PHP

Sometime we need to add or remove fields dynamically in a form.
In this article I'm explaining that how can we add or remove the fields using jQuery.
We just need to click on Add more button and a new field will be added. You also can remove that field.
In this article I also have written the code for inserting the data into MySql.

Here I'm giving an example of tags.

Like If we have requirement of add more tags. And we want to add tags as much as we want. You can do that easily by using this code.
You also can insert all the tags into the MySql database using PHP...  Read More

Share This:


Laravel 5 validation example | How to write validation rules in Laravel | Laravel form validation

While developing the web application we need server side form validations. Laravel does this task beautifully. In this article I'm going to explain how we can do our form validations in Laravel.

We don't need to write the form validations in our Controller code like generally people does.

Here is an example of writing form validation rules in Laravel.

For example here is the form:

When you will submit the form it will go to register route like:

It says the register route has routed to 'postRegister' function which is inside the 'RegisterController'. ...  Read More

Share This: