How To Create A Custom Helper File And Use In Laravel 7 – onlinecode
In this post we will give you information about How To Create A Custom Helper File And Use In Laravel 7 – onlinecode. Hear we will give you detail about How To Create A Custom Helper File And Use In Laravel 7 – onlinecodeAnd how to use it also give you demo for it if it is necessary.
In this article, We will discuss how to create a custom helper file and use in Laravel 7. 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
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.
"autoload": {
"files": [
"app/helper.php"
],After the above changes, we will run the following command.
composer dump-autoload
Step3: How to use the custom helper function in Controller or Views.
How can make an array from the values of another array's key?
In this post we will give you information about How can make an array from the values of another array's key?. Hear we will give you detail about How can make an array from the values of another array's key?And how to use it also give you demo for it if it is necessary.
If you are working on PHP or other PHP framework and you want to create array of another array value. now you can see on following example how can you make array form another multidimensional array key's.
For example you have array like:
$multi = array(
['1'] => array('id'=>1,'name'=>'hardik'),
['2'] => array('id'=>1,'name'=>'vimal'),
['3'] => array('id'=>1,'name'=>'harshad'),
)
but if you want to this multi-dimensional array just like this way:
$test = array('hardik','vimal','harshad');
so, we can make this type of array from multi-dimensional array using array_column() funtion.
you can use this function easy as under.
$result = array_column($multi, 'name');
Try this..........
Hope this code and post will helped you for implement How can make an array from the values of another array's key?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
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
namespace AppHttpControllers;
use IlluminateHttpRequest;
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
$where= array('id'=>$product->product_category_id);
$data = getTableWhere('categories',$where);
{{ $data->title }}
@endphpSo finally, I hope you like this article.Read AlsoLaravel 6 CRUD (Create Read Update Delete) Tutorial For Beginners
Laravel 7 CRUD Operation With Ajax Example
Laravel 6 CRUD Operation With Ajax Example
Laravel 6 Pagination Example Tutorial
Laravel 7 Pagination Example Tutorial
Hope this code and post will helped you for implement How To Create A Custom Helper File And Use In Laravel 7 – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs