Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
DominikStyp / SynchronizeWithPivot.php
Created October 17, 2024 13:27
Laravel 10: synchronize MorphToMany relation WITH PIVOT VALUES
<?php
$roleIds = Role::whereIn('name', ['admin', 'moderator'])->pluck('id')->toArray();
// if pivot table relies on some additional value like in the example 'brand_slug' => 'some-slug'
// we need to use syncWithPivotValues() where we're gonna update pivot records with 'brand_slug' values
$user->brandRoles($brandSlug)->syncWithPivotValues(
$roleIds,
[
'brand_slug' => $brandSlug
@DominikStyp
DominikStyp / PostRequestToApiTest.php
Created October 8, 2024 16:06
Laravel 10: Test POST Request to API example
<?php
class PostRequestToApiTest extends TestCase {
public function test_post_request_to_api(): void {
$route = route('passport.token');
$formData = [
'grant_type' => 'password',
'client_id' => 1,
@DominikStyp
DominikStyp / remove_glow_from_focused_element.scss
Last active August 28, 2024 12:40
CSS/SCSS: Remove glow from focused element, and black border fill
@mixin remove-default-browser-outline {
&, &:focus, &:active {
outline: 0; /* this one is responsible for black border in Chrome */
-webkit-appearance: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
box-shadow: none;
-moz-box-shadow: none; /* this one is responsible for glow in Chrome */
-webkit-box-shadow: none;
text-shadow: none;
@DominikStyp
DominikStyp / CheckSomethingTest.php
Last active July 31, 2024 17:24
Laravel: test log output with hiding sensitive data, and check if the log file has the proper string
<?php
namespace Tests\Feature;
use App\Helpers\SensitiveInputDataRemover;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
class CheckSomethingTest extends TestCase
{
@DominikStyp
DominikStyp / SomeTest.php
Created July 31, 2024 16:33
Laravel: Mock production enviroment in tests
<?php
namespace Tests\Feature;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
class SomeTest extends TestCase
{
/**
@DominikStyp
DominikStyp / lateral join.md
Created July 20, 2024 16:34 — forked from Laratipsofficial/lateral join.md
Implementing lateral join in Laravel

Implementing lateral join in Laravel

Making of joinLateral macro

use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Expression

Builder::macro('joinLateral', function ($query, $as, $type = 'inner') {
    [$query, $bindings] = $this->createSub($query);
@DominikStyp
DominikStyp / makeDatabaseBackup.sh
Last active June 27, 2024 11:10
Backup MySQL database from the docker container to the output SQL file
#!/bin/bash
# REQUIRES ROOT ACCESS TO DUMP ALL THE PROCEDURES TRIGGERS ETC.
source .env
DATE_TIME=$(date +%Y-%m-%d__%H_%M_%S)
BACKUP_FILE="${DB_DATABASE}_backup_${DATE_TIME}.sql"
docker exec project-mysql_database-1 mysqldump -h $DB_HOST -u root -p"$DB_ROOT_PASSWORD" $DB_DATABASE --single-transaction --routines --triggers > $BACKUP_FILE
@DominikStyp
DominikStyp / importDatabaseFromSQLFile.sh
Created June 12, 2024 13:08
Import database to MySQL docker container
#!/bin/bash
DB_CONTAINER_NAME = 'mysql-container-1'
DB_PASSWORD = 'secret'
DB_NAME = 'db'
DB_USER = 'db_user'
DB_PORT = 3306
DB_HOST = 'db-host'
docker exec -i $DB_CONTAINER_NAME mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASSWORD $DB_NAME < dump.sql
@DominikStyp
DominikStyp / CustomLogServiceProvider.php
Created June 4, 2024 13:30
Laravel: How to change/swap container instance behind the Facade for example Log
<?php
namespace App\Providers;
use App\Patches\LogManager;
use Illuminate\Log\LogServiceProvider;
use Illuminate\Support\Facades\Log;
class CustomLogServiceProvider extends LogServiceProvider
{
@DominikStyp
DominikStyp / git-show-contributors-details.sh
Last active June 18, 2024 11:24
Git: show number of lines and commits per contributor, count only files: php|js|css|sass|scss
#!/bin/bash
# provide directory of the repo
cd $1;
echo "------------- COMMITS ------------";
echo " ";
git log --pretty=format:"%an" | sort | uniq -c | sort -nr | awk '
BEGIN { total_commits=0 }