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

How to Remove Characters Except Numbers from a String ?

Following Code will remove all character,special characters including space.

 
$givenNumber = "1245fdfd8454D FDFDF434 3$#$#%";
 
$testingNumber = preg_replace('[D]', '', $givenNumber);
 
echo $testingNumber;
 
Output is : 124584544343

Difference between strstr() and stristr() functions in PHP.

Both functions are used to return or finds the first occurence of a substring from a string, and give all string from first occurence to end of string except than stristr() is case-insensitive.
If no match is found then FALSE will be returned.
Example

 
$email = 'abc@xyz.com';
 
$host = strstr($email, '@');
 
echo $host;
 
output: @xyz.com

stristr() does the same thing in Case-insensitive manner

How to Find Quarter Month From Any Year ?

A Quarter means 1/4 part of year.
In the following example given Script is
use to find which quarter of the year
in the given date.

Assuming given date is 10 December 2015
Following is the Example.
Just Copy & Paste and Enjoy.

$month = 12;
 
$date = 10;
 
$year = 2015;
 
$qm = mktime(0,0,0,$month,$date,$year);
 
echo ceil(date("m", $qm)/3);