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
Laravel 8 Create Custom Helper File Example

Laravel 8 Create Custom Helper File Example

Posted on September 14, 2020December 15, 2022 By XpertPhp No Comments on Laravel 8 Create Custom Helper File Example

In this article, We will discuss how to create a custom helper file and use in Laravel 8(Laravel 8 Create Custom Helper File Example). 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 for create a custom helper function.

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
See also  Laravel 6 Stripe Payment Gateway Integrate Example

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

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 8, laravel 8 example, laravel 8 tutorial

Post navigation

Previous Post: Laravel 8 Image Upload tutorial example
Next Post: Laravel 8 Dropzone Image Upload Example Tutorial

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
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

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