Skip to content

Instantly share code, notes, and snippets.

View yavgel85's full-sized avatar

Eugene Yavgel yavgel85

View GitHub Profile
@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 / 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 / Shorter and more readable syntax.md
Last active June 1, 2021 07:24
Shorter and more readable syntax #laravel #bestpractices
Common syntax Shorter and more readable syntax
Session::get('cart') session('cart')
$request->session()->get('cart') session('cart')
Session::put('cart', $data) session(['cart' => $data])
$request->input('name'), Request::get('name') $request->name, request('name')
return Redirect::back() return back()
is_null($obj->relation) ? null : $obj->relation->id optional($obj->relation)->id
return view('index')->with('title', $title)->with('client', $client) return view('index', compact('title', 'client'))
request->has('value') ? request->value : 'default' request->get('value', 'default')
@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 / 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 / Docker and docker-compose commands.md
Last active November 25, 2021 16:15
Docker and docker-compose commands #docker #command

Docker Compose

start the containers (in detached mode)
docker-compose up -d 
start and build containers
docker-compose up -d --build 
@yavgel85
yavgel85 / Laravel Controller to upload, zip, password protect the zip, save the password encrypted.php
Created March 26, 2021 08:24
Laravel Controller to upload, zip, password protect the zip, save the password encrypted #laravel
<?php
// The issue this solves is to allow the user to upload a document and for us to save the document such that: the document is zip'd, the zip file is given a unique name, the zip file is password protected with a unique password, the password is encrypted for storage in the db table.
// The following will allow for creating one codeset to be called for the creation of all Documents.
// For the moment, we will need to set up IF statements for the statement that creates an instance of $Document
// which will allow for us to create a new entry in the appropriate database table.
public function storeInitDocument($request, $id, $theUserID, $ignore, $tableName){
// This function will determine if the User has uploading any documents. If so, the document's properties will be stored,
// followed by the storage of the physical document with a unique name. The document will be
@yavgel85
yavgel85 / Specified key was too long.php
Last active October 29, 2021 08:14
Specified key was too long #laravel #sql
<?php
// SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
@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;
}