Tuesday, March 8, 2022
how to get last week data in laravel
$users = User::whereBetween(
'created_at', [Carbon::now()->subWeek()->startOfWeek(), Carbon::now()->subWeek()->endOfWeek()] )->get();find days between two dates in laravel
$to = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2015-5-5 3:30:34');
$from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2015-5-6 9:30:34'); $diff_in_days = $to->diffInDays($from); print_r($diff_in_days); // Output: 1Laravel- remove composer package in laravel
In this article we will learn about how to remove the composer package in laravel, we all know how to install it, but when it’s time to remove the package we don’t know.
Well, there is two way to remove or uninstall the composer package from the laravel.
But before moving forward let’s know what composer is, so basically, the composer is a tool that includes all the dependencies and libraries, or simply you can say it manage dependencies.
Step-1 : In this example we will install dompdf composer package with following command:
composer require barryvdh/laravel-dompdfNow we will us the below command to remove the composer
composer remove barryvdh/laravel-dompdfNow you have to clear your config cache , in order to remove the cache file.
php artisan config:clearStep-2 : By removing your package name
To remove your package name open composer.json file, this file is present in root of your project folder.
Now look for barryvdh/laravel-dompdf (look for your package name) it will under require object, remove that one.
then run the below command
composer dump-autoload
How to send notification to anyone
You can send Laravel Notifications not only to a specific user with $user->notify(), but also to anyone you want, via Notification::route(), with so-called “on-demand” notifications:
Notification::route('mail', 'Jhon@gmail.com')
->route('test', '123456')
->route('slack', 'https://hooks.slack.com/services/...')
->notify(new InvoicePaid($invoice));
Saving an Image from URL in PHP/Laravel
Sometimes, need to download an image from a particular URL and use it in the project. It’s easy to go to the page and use right-click button and save the image. But what if you wanted to do it programmatically? The reasons may vary from person to person, developer to developer. If the set of hundreds of image URLs is given and somehow wants to save them into the machine, or need to use this concept in the projects. Then definitely not going to download each one of those files manually.
There are two different approaches to download images from URL which are listed below:
- Using basic file handling.
- Using an HTTP library called cURL.
Both of these approaches come with their own set of merits and demerits.
Using Basic File Handling: This is the fundamental and easiest way to accomplish the task. Just like any other file, start with the creation of an empty file and open it in “write” mode. After that, fetch the content from the source URL and paste it into this file. And it is as simple as it sounds.
From the script, you can figure out on your own what it does.
- Declaring two variables named as $url and $img, representing the source URL and destination file respectively.
- Use file_put_contents() function to write a string to a file that takes two arguments. One is the file name (or path) and the other is the content for that file.
- Use file_get_contents() function to read a file into a string.
<?php $url = 'https://example.png'; $img = 'logo.png'; // Function to write image into filefile_put_contents($img, file_get_contents($url)); echo "File downloaded!" ?>I hope it will help you.
-
https://www.nicesnippets.com/blog/laravel-9-comment-system-example-tutorial
-
Organization : Keeping each site's configuration in separate files makes the server easier to manage, especially when hosting multiple...
-
Laravel Queues allow you to delay a time-consuming task until a later time. By delaying the time-consuming task, you can improve the perfo...