Laravel Tutorials
- What is laravel
- Laravel Installation
- Directory Structure
- htaccess
- Remove public from url
- Artisan Command
- Laravel Configuration
- Routing Configuration
- Namespaces
- Request
- Response
- Controller
- Model
- User Authentication
- Multi User Authentication
- Database Seeding
- Database
- Database Query
- ORM
- One-to-One Relationship
- One-to-Many Relationship
- Many to Many Eloquent Relationship
- Has One Through
- Has Many Through
- Querying Relations
- Middleware
- Laravel Views
- Blade Views
- Print data on view page
- Get Site URL
- Get URL Segment
- Get images from Storage folder
- Clear cache
- Form Class not found
- Flash Message in laravel
- Redirections
- path
- CRUD Projerct
- CRUD in Laravel
- CRUD progran
- Laravel Validation
- Jquery Validation
- Cookie
- Session
- Email Send in laravel
- File uploading
- CSRF Protection
- Helper in Laravel
- Helper Functions
- Guzzle Http Client
- Paypal Payment Gatway Integration
- Cron Job in laravel
- Flash message
- path
- Errors Handling
- Date Format
- Date Format Validation
- Display Image on View Page
- Update User Status using toggle button
- Delete Multiple Records using Checkbox in Laravel?
- Confirmation Before Delete Record
- Delete image from storage
- Remove/Trim Empty & Whitespace From Input Requests
- Block IP Addresses from Accessing Website
- How to Disable New User Registration in Laravel
- Redirect HTTP To HTTPS Using Laravel Middleware
- CKEditor
- slug generate unique
- Prevent Browser's Back Button After Logout
- Datatable dunamically
- encrypt & Decript
- Download File
- Rest API
- Shopping Cart
- Shopping Cart Example
- Dynamic Category-Subcategory Menu
- Ajax Search
- Interview Question
- laravel Tutorilal link
- laravel Tutorilal
Important Links
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(
' ' => '-', '!' => '', '"' => '',
'#' => '', '$' => '', '%' => '',
'&' => '', '\'' => '', '(' => '',
')' => '', '*' => '', '+' => '',
',' => '', '₹' => '', '.' => '',
'/-' => '', ':' => '', ';' => '',
'<' => '', '=' => '', '>' => '',
'?' => '', '@' => '', '[' => '',
'\\' => '', ']' => '', '^' => '',
'_' => '', '`' => '', '{' => '',
'|' => '', '}' => '', '~' => '',
'-----' => '-', '----' => '-', '---' => '-',
'/' => '', '--' => '-', '/_' => '-',
);
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 : & 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