How to search JSON data in MySQL?

If you have JSON type columns in your database and want to run query on JSON type field. You can use the following query.

SELECT JSON_EXTRACT('<json_column>', "$.response") AS name
FROM table
WHERE JSON_EXTRACT('<json_column>', "$.id") > 3

In the above query <json_column> must be replace by your column name.

$.id and $.response contains the JSON key of your columns. You can use for multiple values also.

For example : $.response.name

How to get table name by Model in Laravel 5?

Some times we requires get what’s the table name for particular Model. There is a function defined in Laravel itself name

1
getTable()

Please look below example:

1
2
3
    $transaction = new Transaction;
    $table = $transaction-&gt;getTable();
    print_r($table);

How to redirect from http to https using .htaccess ?

Redirect to https

If you want to redirect each request coming should go to https instead of HTTP without any plugin. then

Step1: Go to root directory of your website and look for .htaccess file. It should be exist in root directory.

Step2: Find the ‘RewriteEngine On’ Add the following code after that.

1
2
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

Here yourdomain stands for website name for which you want to redirect to https.

How to create custom helper functions in Laravel 5?

From the laravel official
Laravel includes a variety of global "helper" PHP functions. Many of these helper functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.

Helpers are pre built utility functions in laravel which we can use directly without any class import/extend. e.g. str_random() – generates a random string of the specified length.
dd() – dumps the given variables and ends execution of the script.

We can also create our own helpers in laravel. Here are the steps.

Step1 : Create a folder in app folder with named Helpers(You can create with other name also).
Step2 : Now create a file with name helpers.php(You can create with other name also) file in Helpers folder
Step3 : Add the utility functions in that file.
Step3 : Now add the open composer.json locate autoload block and add the following line

"files": ["app/Helpers/helpers.php"]

Step3 : Now,load it up with composer – composer dump-autoload

Now you can use your utility functions anywhere in the application.

How to find duplicate rows from query ?

If you want to find duplicate rows from your table use the following query.

SELECT column1,column2 COUNT(*) users GROUP BY column1, column2 HAVING COUNT(*) > 1

Following are steps :

To test the above query on sample table

Step1 : First of all you need a table (Assuming you have database)

1
2
3
4
5
6
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`age` int(11) NOT NULL
)

Step2 : Insert the dummy values

1
2
3
4
5
6
7
8
9
INSERT INTO `users` (`id`, `name`, `email`, `age`) VALUES
(1, 'shyam', 'shyam@example.com', 31),
(2, 'ram', 'ram@xample.com', 23),
(3, 'shyam', 'shyam@example.com', 31),
(4, 'ram', 'ram@xample.com', 23),
(5, 'shyam1', 'shyam@example.com', 31),
(6, 'ram1', 'ram@xample.com', 23),
(7, 'shyam1', 'shyam1@example.com', 31),
(8, 'ram1', 'ram1@xample.com', 23);

Result would be :

name email COUNT(*)
shyam shyam@example.com 2
ram ram@xample.com 2

 

How to display image from database in PHP?

Step1 : Fetch/Retrieve the image from datbase using database functions.
Step2 : Now use the image tag below.

<?php
$convertedImage ='CONVERTED_IMAGE_FROM_DB';
?>
<img src="data:image/jpg|gif|png;charset=utf8;base64,$convertedImage" />
jpg/gif/png depends on the type of image.

How to save image in Database using PHP ?

Step 1: First of all Convert image to binary.

 
<?php
$imageURL ='YOUR_IMAGE_URL'; //or you can upload
$convertedImage = base64_encode(file_get_contents($imageURL));
/** Now store $convertedImage in Database using database functions.**/
 
?>

How to Translate Text to desired language ?

Just follow these steps and copy & paste the code you can translate text easily.

Step 1 : Make a div with id (id is compulsory)
Like

<div id="content">Loading...</div>

Step 2 : Paste the following code to head section under script tag of your webpage.

 

<script type="text/javascript">
google.load("language", "1");
 
function initialize() {
var content = document.getElementById('content');
// Setting the text in the div.
content.innerHTML = '&amp;lt;div id="text"&amp;gt;Place Your Text Here (Hola, me alegro mucho de verte.)&amp;lt;/div&amp;gt;&amp;lt;div id="translation"/&amp;gt;';
 
// Grabbing the text to translate
var text = document.getElementById("text").innerHTML;
 
// Translate from Spanish to English, and have the callback of the request
// put the resulting translation in the "translation" div.
// Note: by putting in an empty string for the source language ('es') then the translation
// will auto-detect the source language.
google.language.translate(text, 'es', 'en', function(result) {
var translated = document.getElementById("translation");
if (result.translation) {
translated.innerHTML = result.translation;
}
});
}
google.setOnLoadCallback(initialize);
</script>

Step 3 : And the final Step is paste the following URL in src of script tag.

<script type="text/javascript" src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0"&gt;&lt;/script>

Above example is translating Spanish to English You can translate as your choice.
Just change the language code in

google.language.translate(text, 'es', 'en', function(result)

es stands for Spanish,
en stands for English
You can find more language code at
http://code.google.com/apis/language/translate/v1/reference.html