Skip to content

Instantly share code, notes, and snippets.

View yavgel85's full-sized avatar

Eugene Yavgel yavgel85

View GitHub Profile
@yavgel85
yavgel85 / When registering routes, you may limit the possible values for a route parameter.php
Created March 26, 2021 08:44
When registering routes, you may limit the possible values for a route parameter #laravel #route
<?php
// When registering routes, you may limit the possible values for a route parameter. In this way you may change your response based on a valid parameter, having an expressive route and not a Query string parameter.
Route::get('stores/{store}/orders/{type?}')
->uses('StoreOrderController@byType')
->name('stores.orders.by-type.index')
->where('type', 'new|fulfilling|pickup|history')
;
@yavgel85
yavgel85 / UrlFormatter.php
Created March 26, 2021 08:42
Url Formatter #laravel #php #helper
// Format a string into a url and translate any characters based on a conversion table
//UrlFormatter
<?php
namespace AppBundle\Formatter;
class UrlFormatter
{
@yavgel85
yavgel85 / TruncateNumber.php
Created March 26, 2021 08:40
Truncate Number #php
<?php
// Truncate Number without rounding the value
function truncate_number($number, $precision = 2)
{
// Zero causes issues, and no need to truncate
if (0 == (int) $number) {
return $number;
}
@yavgel85
yavgel85 / Transform brackets into an Eloquent query.php
Created March 26, 2021 08:39
Transform brackets into an Eloquent query #laravel #db
<?php
// What if you have and-or mix in your SQL query, like this:
// ... WHERE (gender = 'Male' and age >= 18) or (gender = 'Female' and age >= 65) How to translate it into Eloquent?
// This is the wrong way:
$q->where('gender', 'Male');
$q->orWhere('age', '>=', 18);
$q->where('gender', 'Female');
@yavgel85
yavgel85 / Size ordering helper.php
Created March 26, 2021 08:37
Size ordering helper #laravel #helper
<?php
namespace AppBundle\Helper;
class SizeOrderHelper
{
private const PATTERNS = [
'^[0-9]+$' => false,
'^[0-9\/]+$' => false,
'^[0-9]+yrs$' => false,
@yavgel85
yavgel85 / search_query.php
Created March 26, 2021 08:35
Search query #laravel #scope
<?php
// search query from scope in laravel model
public function scopeSearchResults($query)
{
return $query->when(!empty(request()->input('location', 0)), function($query) {
$query->whereHas('location', function($query) {
$query->whereId(request()->input('location'));
});
})
@yavgel85
yavgel85 / Random Password.php
Created March 26, 2021 08:33
Random Password #php #auth
function random_password($length = 12)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
return substr(str_shuffle($chars), 0, $length);
}
@yavgel85
yavgel85 / Query Builder Macros.php
Last active June 14, 2022 14:09
whereLike macro #laravel #queryBuilder #macro
// Create custom method for Query Builder
<?php
// You can put it the boot method of App\Providers\AppServiceProvider or a service provider of your own.
//Usage Model::whereLike('name', ['mike', 'joe'])->get()
//Usage Model::whereLike('message.type', ['text', 'sms'])->get();
Builder::macro('whereLike', function ($attributes, $terms) {
@yavgel85
yavgel85 / PricePointHelper.php
Created March 26, 2021 08:31
PricePointHelper #laravel #helper
# PricePointHelper
<?php
namespace AppBundle\Helper;
class PricePointHelper
{
/**
* Round a price up to the next price point
* @param float|null $price
@yavgel85
yavgel85 / Laravel validation after hook in form request file.php
Created March 26, 2021 08:28
Laravel validation after hook in form request file #laravel #validation
<?php
// if you want to run custom validation having a long function in laravel request file, you can use the following tricks to get the things done using withValidator method.
class StoreBlogPost extends FormRequest
{
public function authorize()
{
return true;
}