mantas-done / laravel-sparkles
Helpers for Laravel
This package is auto-updated.
Last update: 2024-03-12 08:56:24 UTC
README
Laravel Sparkles is a collection of helpers that are often used in my Laravel project.
Instalation
composer require mantas-done/laravel-sparkles 1.*
Usage
Get currently logged in user (the same as Auth::user())
user();
Current user IP address
ip();
delete()
Delete all models in collection
delete($user->comments); // equals to this foreach ($user->comments as $comment) { $comment->delete(); }
Delete one model
delete($user->profilePicture); // equals to if ($user->profilePicture) { $user->profilePicture->delete(); }
forceDelete()
The same as delete(), but force deletes records (for soft deletes).
forceDelete($user->comments); // equals to this foreach ($user->comments as $comment) { $comment->forceDelete(); }
bustCache()
Bust cache of files: CSS, images...
It adds md5 hash of a file.
echo bustCache('/css/app.css'); // /css/app.css?v=0c39e0bb074f164bc2e7e3e5854927c5
twoDigits()
Display currency with two digits.
twoDigits(1.2); // 1.20 twoDigits(1.234); // 1.23 twoDigits(1, '$'); // $1.00 twoDigits(-1, '$'); // -$1.00
Cookies
Simplifies working with Laravel cookies. You can call those functions from anywhere in your code unlike Laravel cookie implementation.
cookieSet('name', 'value', 60); cookieGet('name'); cookieForget('name');
paginate()
Paginates collection or array and returns default Laravel paginator.
$users_array = [[ 'email' => 'abc@abc.com', 'name' => 'Abc', ], [ 'email' => 'bcd@bcd.com', 'name' => 'Bcd', ]]; $users = paginate($users_array); return view('users.index', compact('users');
validate()
Controller request data validation
// use UserController extends Controller { public function store() { validate([ 'email' => 'required|email', ]); // save user } } // instead of UserController extends Controller { public function store(Request $request) { $request->validate([ 'email' => 'required|email', ]); // save user } }
Function data validation
class Comment { public static function saveComment($email, $comment) { validate([ 'email' => 'required|email', 'comment' => 'required|string|between:1,256', ], compact('email', 'comment')); // save comment } }
deleteAllDirectoryFilesExceptGitignore()
When folder is created in /storag/app folder, usually it has .gitignore file added to it.
When migrating and seeding database in development, it is beneficial to delete all files from some folders.
deleteAllDirectoryFilesExceptGitignore('/storage/app/uploads');
faker()
Get instance of a faker
echo faker()->email;
Contributing
I decided to share my helpers when I saw that similar concepts got implemented in Laravel, like artisan fresh and now().
I believe that other developers have their own gems hidden in their code. If you would like to share one of them, please create a pull request to master branch.