What is Helper in laravel?

composer require laravel/helpers

helper function is used for array, url, route, path etc. some basic helper function like date format in our project. it is many time require. so i think it's better we create our helper function use everywhere same code.

File name : app/helpers.php

you need to create app/helpers.php in your laravel project. and add the following code in this file:


<?php

function changeDateFormate($date,$date_format){
return \Carbon\Carbon::createFromFormat('Y-m-d', $date)->format($date_format);
}

function productImagePath($image_name)
{
return public_path('images/products/'.$image_name);
}

Step 2: Add File Path In composer.json File

In this step, you have to put path of helpers file,so basically open composer.json file and put following code in that file


File name : composer.json

"autoload": {

"classmap": [

...

],

"psr-4": {

"App\\": "app/"

},

"files": [

"app/helpers.php"

]

},

Step 3: Run Command

composer dump-autoload


Example 1

$imageName = 'example.png';
$fullpath = productImagePath($imageName);
print_r($fullpath);

Example 2

Read Also: Laravel 6 REST API with Passport Tutorial
{{ changeDateFormate(date('Y-m-d'),'m/d/Y') }}

How to create own custom helper function in laravel?

Create helpers.php File

File name : index.php

php artisan make:helper Helper

It will create a Helper.php file in App\Helpers

View page

File name : student.blade.php

@php

$where= array('id'=>$row->id);
$data = getStudInfo('students',$where);
{{ $data->name }}

@endphp

Helper Function

File name : helper.php

<?php

if(!function_exists('getStudInfo')) {

function getStudInfo($table,$where) {
$data = \DB::table($table)
->select(\DB::raw('*'))
->where($where)
->first();
return $data;
}
}

?>

Add helper file in the composer.json

File name : index.php

"autoload": {
"files": [
"app/helper.php"
],

File name : index.php

composer dump-autoload

For Controller

File name : StudentController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function index()
{
$where= array('id'=>$row->id);
$data = getTableWhere('students',$where);

echo "<pre>";
print_r($data);
echo "</pre>"; die;

return view('admin.student-view', compact('result','data'));
}
}
?>

Helper Function

File name : helper.php

<?php

function imploadValue($types){
$strTypes = implode(",", $types);
return $strTypes;
}

function explodeValue($types){
$strTypes = explode(",", $types);
return $strTypes;
}

function random_code(){

return rand(1111, 9999);
}

function remove_special_char($text) {

$t = $text;

$specChars = array(
' ' => '-', '!' => '', '"' => '',
'#' => '', '$' => '', '%' => '',
'&amp;' => '', '\'' => '', '(' => '',
')' => '', '*' => '', '+' => '',
',' => '', '₹' => '', '.' => '',
'/-' => '', ':' => '', ';' => '',
'<' => '', '=' => '', '>' => '',
'?' => '', '@' => '', '[' => '',
'\\' => '', ']' => '', '^' => '',
'_' => '', '`' => '', '{' => '',
'|' => '', '}' => '', '~' => '',
'-----' => '-', '----' => '-', '---' => '-',
'/' => '', '--' => '-', '/_' => '-',

);

foreach ($specChars as $k => $v) {
$t = str_replace($k, $v, $t);
}

return $t;
}

Example 1:

The random_code() function is used to generate a random digits code.

File name : index.php

$randomno = random_code();
print_r($randomno);

Example 2:

The remove_special_char function is used to remove the special character from string.

File name : index.php

$str = "itechxpert : &amp; Sana mahtab is the *director of this @$ company."
$remove = remove_special_char($str);

print_r($remove);

//output
itechxpert : sana mahtab is the director of this company





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial