Skip to content

Instantly share code, notes, and snippets.

View arlanram's full-sized avatar

arlan arlanram

View GitHub Profile
@arlanram
arlanram / translate.js
Last active April 14, 2021 19:21
Pure localization for JS with unlimited JSON. simple, no need to require dependencies such i18, etc...
translate(key) {
let res = key.split('.').reduce((a, b) => a[b], t.data)
return res ? localStorage.getItem('translation') == 2 ? res[1] : res[0] : 'Translation not found'
}
//if you want to call tranlsate function from anywhere of your Vue application, you can use mixins
Vue.mixin({
methods: {
translate(key) {
let res = key.split('.').reduce((a, b) => a[b], t.data)
@arlanram
arlanram / index.js
Created March 29, 2021 20:17
JavaScript (JS): global function for making query to API and storing response in Cache Storage with query strings and Vuex state for reactivity
//args: API url, commit name, params (qs), headers
export function query(url, commit, params, headers = null) {
//build new url
const q = new URL(process.env.VUE_APP_API + url)
//if params exists then append it to URL with URLSearchParams
if (params) {
q.search = new URLSearchParams(params).toString()
}
@arlanram
arlanram / PostController.php
Last active March 28, 2021 09:42
Laravel: search for items and filter by any statement and dynamically add or remove them with depended conditions
<?php
namespace App\Http\Controllers;
class PostController extends Controller
{
//i recommend to use shortened version like sid, hid and tid
protected array $ids = [
'some_id',
'hello_id',
@arlanram
arlanram / Handler.php
Created March 28, 2021 09:05
Laravel: global handler with clean json structure and get complete error stacktrace from native server. Use api.php in routes dir
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
public function register()
{
@arlanram
arlanram / CacheQuery.php
Last active July 25, 2024 18:41
Laravel: Cache::remember() with middleware to cache all incoming request and handle them to cache storage or make request if key not found
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Cache;
class CacheQuery
{
public function handle($request, Closure $next)