Skip to content

Instantly share code, notes, and snippets.

@sam-ngu
sam-ngu / GenerateSitemap.php
Last active September 22, 2023 01:54
Laravel php artisan generate sitemap command
<?php
namespace App\Console\Commands;
use App\Models\Course;
use Illuminate\Console\Command;
use Spatie\Browsershot\Browsershot;
use Spatie\Crawler\Crawler;
use Spatie\Sitemap\SitemapGenerator;
use Spatie\Sitemap\Tags\Url;
@sam-ngu
sam-ngu / index.html
Last active September 22, 2023 01:53
Vanilla JS: Convert HTML elements to PDF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
section {
background-color: #80b8e9;
@sam-ngu
sam-ngu / SitewideSearchController.php
Last active September 22, 2023 01:53
An implementation of full site search in Laravel
<?php
namespace App\Http\Controllers;
use App\Http\Resources\SiteSearchResource;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
@sam-ngu
sam-ngu / CollectionHelper.php
Last active September 22, 2023 01:49
Laravel Collection Helper: paginate collection
<?php
namespace App\Helpers\General;
use Illuminate\Container\Container;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
@sam-ngu
sam-ngu / deploy.sh
Created May 23, 2021 05:28
Bash script to deploy a React app to Github pages
#!/usr/bin/env sh
# abort on errors
set -e
# build
npm run build
# navigate into the build output directory
cd build
@sam-ngu
sam-ngu / Composer.php
Created November 10, 2020 01:40
Laravel helper class to run composer command programmatically.
<?php
namespace App;
class Composer extends \Illuminate\Support\Composer
{
public function run(array $command)
{
@sam-ngu
sam-ngu / laravel-mail-components.blade.php
Created May 21, 2019 00:59
list of laravel mail markdown components
@component('mail::message')
# Introduction
The body of your message.
@component('mail::button', ['url' => ''])
Button Text
@endcomponent
@component('mail::panel')
@sam-ngu
sam-ngu / iterate.php
Last active June 24, 2021 11:54
PHP iterator to recursively iterate through a folder.
<?php
$folderPath = __DIR__ . '/path/to/folder/';
try {
$dirIterator = new \RecursiveDirectoryIterator($folderPath);
/** @var \RecursiveDirectoryIterator | \RecursiveIteratorIterator $it */
$it = new \RecursiveIteratorIterator($dirIterator);
// the valid() method checks if current position is valid eg there is a valid file or directory at the current position
@sam-ngu
sam-ngu / route_group_array_syntax.php
Last active June 16, 2021 08:08
Laravel Route group: array syntax vs method syntax. Blog article:
<?php
// using array syntax
Route::group([
'middleware' => [
'auth:api',
\App\Http\Middleware\RedirectIfAuthenticated::class,
],
'prefix' => 'heyaa', // adding url prefix to all routes in this group
'as' => 'users.', // adding route name prefix to all routes in this group
'namespace' => "\App\Http\Controllers",
@sam-ngu
sam-ngu / default_conda_env.sh
Created June 14, 2021 03:47
How to change default anaconda env in Unix based system.
# in ~/.bashrc
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/sam/anaconda3/bin/conda' shel$
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else