Implement Helper Functions in Laravel.

I am CS Student currently perusing my bachelor degree. I love coding and implementing new logics. Aside from that I'm also a hardware lover and has great interest in IOT.
Search for a command to run...

I am CS Student currently perusing my bachelor degree. I love coding and implementing new logics. Aside from that I'm also a hardware lover and has great interest in IOT.
No comments yet. Be the first to comment.
Create a .htaccess in the root of the directory and append this line anywhere in the file. AddHandler application/x-httpd-alt-php74___lsphp .php Here 74 represents PHP version 7.4. If we want 8.1, it would be 81.
We are going to create a local domain like app.local, Create a self-signed wildcard certificate which will works for subdomains as well and point the domain to a locally running site. Generate a self signed certificate https://www.brainbytez.nl/tutor...
GIT_COMMITTER_DATE="Sun Jul 16 14:00 2023 +0545" git commit --amend --date="Sun Jul 16 14:00 2023 +0545"
mkdir output && for file in *.heic; do heif-convert -q 100 "$file" "output/${file/%.heic/.jpg}"; done mkdir -p output && for file in *; do [[ $file == *.heic ]] && heif-convert -q 100 "$file" "output/${file/%.heic/.jpg}" || cp "$file" output/; done
https://dev.to/cloudx/how-to-use-puppeteer-inside-a-docker-container-568c Dockerfile FROM node:slim # We don't need the standalone Chromium ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true # Install Google Chrome Stable and fonts # Note: this installs the...
Helpers functions are very useful and effective in Laravel. It's main advantages is that they can be called from everywhere. I mean everywhere, its everywhere. You can call it from view , you can call from controller, everywhere. It can help in various ways like sendmail() function which can send mail. Another example could be active menu items for navbar.
Now let's see how to do it.
First, create a file named helpers.php in app\Helpers. You can now write any function on that file, which can be called from anywhere. But we are not done yet. Upto now we only made the file but Laravel doesn't know about it. So we have to register it to get autoloaded automatically. For that, we have to add some lines of codes in autoload section on the composer.json file at the root directory.
"autoload": {
"files": [
"app/Helpers/helpers.php"
],
"psr-4": {
.
.
.
}
},
Now run this command to regenerate the Laravel's optimized autoload files.
composer dump-autoload
Now you can go ahead and define your helpers functions. Remember to open a php tag <?php at the start. Here's one example to start with.
<?php
use Illuminate\Http\Request;
if (!function_exists('sendSimpleMail')) {
function sendSimpleMail($to, $from_name, $from_address, $subject, $content)
{
Mail::send('emails.simplemail', ['content' => $content], function ($message) use ($to, $from_name, $from_address, $subject, $content) {
$message->from($from_address, $from_name ? $from_name : env(APP_NAME));
$message->subject($subject);
$message->to($to);
});
return response()->json(['message' => 'Request completed']);
}
}
This example function is used to send an simple email with the parameters provided. It's always a good idea to check if the function name already exists before defining it with function_exists() function.
And now you can use your helper functions wherever you want.
Happy Coding.