This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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') | |
; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Format a string into a url and translate any characters based on a conversion table | |
//UrlFormatter | |
<?php | |
namespace AppBundle\Formatter; | |
class UrlFormatter | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Helper; | |
class SizeOrderHelper | |
{ | |
private const PATTERNS = [ | |
'^[0-9]+$' => false, | |
'^[0-9\/]+$' => false, | |
'^[0-9]+yrs$' => false, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
}); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function random_password($length = 12) | |
{ | |
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"; | |
return substr(str_shuffle($chars), 0, $length); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PricePointHelper | |
<?php | |
namespace AppBundle\Helper; | |
class PricePointHelper | |
{ | |
/** | |
* Round a price up to the next price point | |
* @param float|null $price |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |