We have build a package with some helpers that are usefull in almost every project. We use this package in all our projects. For ourself and for our customers. If you miss anything, create an issue and will will add it as soon as possible.
You can install the package via composer:
composer require marshmallow/helpers
This helper will make generating .csv files extremly ease. Please check the examples below. You can use the store method to store it in a location of your choosing. You can use the download method if you just want to download the file. If you wish to store and download the generated .csv file you can use the storeAndDownload method.
$headers = ['Column 1', 'Column 2', 'Column 3'];
$data = [
[1, 2, 3],
[4, 5, 6],
];
return CSV::headers($headers)
->data($data)
->delimiter(';')
->setFilename('generated-csv-1234')
->storeAndDownload();
You can use a collection as your data attribute. By default it will map all the data in the collection sequence. If you wish to edit the collection data, you can add a callback method to the data method. Please note that this will give you every row as an array
value.
use Marshmallow\HelperFunctions\Facades\CSV;
CSV::headers($headers)
->data($data, function (array $row) {
return [
$row['name'],
$row['slug'],
$row['created_at'],
];
})
->storeAndDownload();
The following methods are available for storing and downloading the generated csv file.
$csv->download();
$csv->store();
$csv->storeAndDownload();
$csv->stream();
This is a public helper function that is available within your Laravel application. This function can be used in all your PHP and Blade files.
percentage(47, App\Post::get()); // 63.829787234043
The Str helper extends the helper from Laravel. So you have all the methods available in the Laravel helper available as well. Check the Laravel documentation for all the available methods.
This method is super handy for concatenating strings separated by a comma, but use another value for the last item. Checkout the example below.
Str::join([
'Marshmallow',
'Stef van Esch',
'Mr Mallow'
]);
// Marshmallow, Stef van Esch and Mr Mallow
We have added on the default Str::random()
of Laravel. We've added a second parameter which is an array of characters that should be ignored. We also have build in a couple of presets like lowercase
which will make sure the random string won't contain any lowercase characters.
Str::random($limit = 16, $ignore = [
/**
* Custom items
*/
'A','B', 'C', 'D',
/**
* Presets
*/
'lowercase', // Will ignore all lowercase characters.
'uppercase', // Will ignore all uppercase characters.
'letters', // Will ignore all letters.
'numbers', // Will ignore all numbers.
'similar', // Will ignore all numbers and letters that have been marked as similar.
]);
Str::cleanPhoneNumber()
Return a cleaned phonenumber with the country code. You can choose to return the phonenumber with a +
or 00
at the start of the phonenumber.
Str::phonenumberWithCountryCode('0628998954')
// response: +31628998954
Str::phonenumberWithCountryCode(
'0031628998954',
$country_code = '31',
$use_plus_instead_of_zeros = false
);
// response: 0031628998954
Str::numbersOnly()
Str::numbersAndLettersOnly()
Str::readmore(
$string,
$lenght_first_part,
$return_this_part = null
);
Str::paragraphsAsArray($string);
Str::getFirstParagraph(
$string,
$number_of_paragraphs = 1,
$return_array = false
);
Str::getAllButFirstParagraph(
$string,
$number_of_paragraphs_to_skip = 1,
$return_array = false
);
We have a trait available that gives you some extra options when creating migrations. Add the MigrationHelper
trait to you migration to make use of these options.
use Marshmallow\HelperFunctions\Traits\MigrationHelper;
class CreateProductTable extends Migration
{
use MigrationHelper;
This method was added because when a database that already has a products table and later will use our Product
package, the migrations will through error's.
$this->createColumnIfDoesntExist(
'products', 'deleted_at', function (Blueprint $table) {
$table->softDeletes();
}
);
URL::isInternal($url)
URL::isCurrent($url)
URL::buildFromArray($array)
URL::isNova($request)
URL::isNotNova($request)
This method will store a pretty array in a file. With this method is possible to generate config files.
Arrayable::storeInFile(array $array, string $file_location);
BuilderHelper::published
will filter on database columns if something is published.
public function scopePublished (Builder $builder)
{
BuilderHelper::published(
$builder,
$valid_from_column,
$valid_till_column
);
}
For the review stars you can call ReviewHelper::ratingToStars(4.5)
. By default the ReviewHelper will think you are using a max rating of 5, support half star rating and return a string of FontAwesome icons. You can overule this behaviour by;
Create the config file config/review.php
and specify your needs:
return [
'max_rating' => 10,
'full_star' => '+ ',
'half_star' => '* ',
'empty_star' => '- ',
];
Or you can provide the same config array as a second parameter to the ratingToStars
method like so;
ReviewHelper::ratingToStars(4.5, [
'max_rating' => 10,
'full_star' => '+ ',
'half_star' => '* ',
'empty_star' => '- ',
])
Grouper is a super handy helper when you need to split your queries results in to groups. We use this with blogs a lot. The first row might have 3 results, the second row will have 1 result, en the third row will have 3 items again. Please see the example below.
use Marshmallow\HelperFunctions\Facades\Collection;
$grouper = Collection::createGrouper(Blog::get(), $structure_array = [
'first' => 3,
'second' => 1,
'third' => 2,
]);
As you can see in the example, the structure_array
has a name as the key. This is done so when you are looping through your groups, you are able the test which group you are currently looping.
foreach ($grouper as $group) {
if ($group->is('first')) {
// Add your template for 3 items
}
if ($group->is('second')) {
// Add your template for 1 item
}
}
Looping through the group items works just like you expect when working with collections.
foreach ($grouper as $group) {
foreach ($group as $blog) {
//
}
}
Please see CHANGELOG for more information what has changed recently.
composer test
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.