Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How to create a custom helper file and use in Laravel 5.8

How to create a custom helper file and use in Laravel 5.8

Posted on April 21, 2019December 17, 2022 By XpertPhp 4 Comments on How to create a custom helper file and use in Laravel 5.8

In this article, We will discuss how to create a custom helper file and use in Laravel 5.8. laravel provides lot’s up functionality but sometimes we need to custom functionality for our project or task. we will take the latest example and learn how to use a custom helper function. so let’s follow the below step.

Step1: Create the helper file.

first, we will create helper file in the “your-project-directory/app/helper.php”. put some code in this file. here this file we use the dynamic table with where condition. because we need the category name according to the product category id. so finally, we will get the category name using the custom helper function.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
 
if(!function_exists('getTableWhere')) {
 
    function getTableWhere($table,$where) {
        $data = \DB::table($table)
            ->select(\DB::raw('*'))
            ->where($where)
            ->first();
        return $data;
    }
}
 
?>

Step2: Add helper file in the composer.json file.

Now, we will add the path in the composer.json file at autoload array section.

PHP
1
2
3
4
"autoload": {
        "files": [
    "app/helper.php"
],

After the above changes, we will run the following command.

1
composer dump-autoload

Step3: How to use the custom helper function in Controller or Views.

See also  Laravel 8 Send Email Example Tutorial

now, we can use the custom helper function in Controller or Views. we can get the categories name in Controller or Views using the call getTableWhere helper function and pass parameter.

Controller Example

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
  public function index()
  {
$where= array('id'=>$product->product_category_id);
$data = getTableWhere('categories',$where);
echo "<pre>";
print_r($data);
echo "</pre>"; die;
    return view('product.view', compact('result','data'));
  }
}
?>

View Example

PHP
1
2
3
4
5
6
7
@php  
 
$where= array('id'=>$product->product_category_id);
$data = getTableWhere('categories',$where);
{{ $data->title }}
 
@endphp

So finally, I hope you like this article.

Laravel Tags:Laravel Framework, laravel from scratch

Post navigation

Previous Post: How to convert stdclass object to array in laravel 5.8
Next Post: How can get the session value on the controller constructor in the laravel

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com