Skip to content

Instantly share code, notes, and snippets.

View yavgel85's full-sized avatar

Eugene Yavgel yavgel85

View GitHub Profile
@yavgel85
yavgel85 / Set up test traits dynamically.php
Created March 25, 2021 20:08
Set up test traits dynamically #php #laravel #test
<?php
// Setup:
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function setUp(): void
{
@yavgel85
yavgel85 / Run these commands when deploying your laravel app to production.php
Created March 25, 2021 20:18
Run these commands when deploying your laravel app to production #php #laravel
composer install --optimize-autoloader --no-dev
php artisan route:cache
php artisan config:cache
php artisan view:cache
php artisan event:cache
@yavgel85
yavgel85 / local_login.php
Last active March 26, 2021 08:16
Working locally and don't want to log in manually every time when working with randomized database seeds? #laravel #auth
<?php
// routes/web.php
Route::get('auto-login', function() {
// Only available in local environment
abort_unless(app()->environment('local'), 403);
// Login with first user from DB seeds
auth()->login(User::first());
@yavgel85
yavgel85 / Get the SQL of a Query Builder without the question marks.php
Last active March 26, 2021 08:16
Get the SQL of a Query Builder without the question marks #laravel #queryBuilder
<?php
use Illuminate\Database\Eloquent\Builder;
Builder::macro('toSqlWithBindings', function () {
$bindings = array_map(
fn ($value) => is_numeric($value) ? $value : "'{$value}'",
$this->getBindings()
);
@yavgel85
yavgel85 / Creating the database.php
Last active March 26, 2021 08:18
Creating the database #laravel #db #command
<?php
// After setting the environment variables, we can go ahead and create a new Database. We can utilize the power of PHP artisan here with Laravel commands.
# Step 1
// Fire up the command line and navigate to the project’s root directory Run the following command – php artisan make:command CreateMySQLDb
# Step 2
// In the code editor file explorer, locate the new command file which is named CreateMySQLDb.php within the following folder app/Console/Commands. Edit the contents to look like the following snippet and save it.
@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;
}
@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 / 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 / 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 / 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,