Supercharge Your Laravel Development and Get AI to Understand Your Models
Hey there, Laravel enthusiasts! Today, I’m diving into a nifty trick that’ll make getting AI to understand your Laravel model structures a breeze. We’ll harness the power of bash scripting and AI to analyze our migrations quickly and efficiently. Let’s get started!
The Problem: Getting AI to Return Code For your Project Setup Easily
As our Laravel projects grow, so does the complexity of our database structures. Migrations pile up, relationships intertwine, and before you know it, you’re drowning in a sea of Schema::create
and up
functions. Wouldn’t it be great to get a bird’s-eye view of our models without manually sifting through dozens of files? What if you could get AI to understand your Laravel project without adding every file to the chat window?
The Solution: Bash + AI = Developer’s Best Friend
Here’s where our dynamic duo comes in a clever bash one-liner and your favorite AI chat tool (like ChatGPT or Claude). We’ll use bash to extract the relevant parts of our migrations and then feed that information to an AI for analysis and insights.
Step 1: The Magical Bash One-Liner
Here’s the one-liner code to copy and paste:
for file in *.php; do echo -e "\n\`\`\`\n${file%.*}:\n\`\`\`\n"; sed -n '/Schema::create/,/^ }/p' "$file"; echo -e "\n\`\`\`"; sed -n '/public function up/,/^ }/p' "$file" | sed '1d;$d'; echo -e "\`\`\`\n"; done
First, let’s break down our bash sorcery to understand what’s going on in an easier-to-read format:
for file in *.php; do
echo -e "\n\`\`\`\n${file%.*}:\n\`\`\`\n"
sed -n '/Schema::create/,/^ }/p' "$file"
echo -e "\n\`\`\`"
sed -n '/public function up/,/^ }/p' "$file" | sed '1d;$d'
echo -e "\`\`\`\n"
done
Just add | pbcopy to have it copied directly to your clipboard on Mac:
for file in *.php; do echo -e "\n\`\`\`\n${file%.*}:\n\`\`\`\n"; sed -n '/Schema::create/,/^ }/p' "$file"; echo -e "\n\`\`\`"; sed -n '/public function up/,/^ }/p' "$file" | sed '1d;$d'; echo -e "\`\`\`\n"; done | pbcopy
This command does the following:
- Loops through all PHP files in the current directory
- Extracts the
Schema::create
function contents - Extracts the
up
function contents (minus the function declaration) - Formats the output with the filename and proper Markdown code blocks
Step 2: Running the Command
- Open your terminal and navigate to your Laravel project’s migrations directory.
- Copy the bash one-liner above.
- Paste it into your terminal and hit Enter.
- Watch as it generates a nicely formatted output of your migrations!
Step 3: Feeding the Output to AI
Please copy the entire output from your terminal and paste it into your AI chat of choice with your preferred prompt. I usually start with this prompt:
"Analyze these Laravel migrations and understand the database and model structure, including relationships. For all conversations in this chat, refer to these migrations:"
{output here}
Generate a diagram
Here are some additional prompts you can optionally pair with the output to extract valuable insights and improvements:
- “Identify any potential relationships between these models based on the migrations.”
- “Suggest improvements or potential issues in this database design.”
- “Take this example of JSON output and generate the code to create the [model name] with its relations.”
The Benefits: Why This Approach Rocks
- Time-saver: No more manual searching through migration files or copying content manually or individually.
- Comprehensive view: Get a complete picture of your database structure in one go.
- AI-powered insights: Leverage AI to spot patterns, suggest optimizations, explain complex relationships, and generate code faster.
- Learning tool: Great for understanding legacy projects or onboarding new team members.
Wrapping Up
There you have it, folks! With this simple bash one-liner and the power of AI, you can transform how you analyze and understand your Laravel database structures and output code. Give it a try on your next project, and watch your productivity soar!
Remember, tools like these are meant to augment your skills, not replace them. Always review AI suggestions critically and trust your developer instincts.
How to Move Files by Partial File Name to New Directory on Mac using a shell script
Streamline Your Photo Editing Workflow: How to Use a Shell Script to Prioritize Selected Images
When it comes to photo editing, efficiency is key. Recently, I faced a challenge where a subject picked out 100 priority photos from a larger batch. Given the sheer number of images, manually sorting them wasn’t practical. All pictures from my SLR were named in the DCIM_# format, so I noted the last five numbers of the selected files. To streamline the process, I wrote a shell script that automatically moves the chosen files to a new folder, allowing me to focus on the priority images first. I’m sharing this script to help others enhance their photo editing workflow.
Step-by-Step Instructions on How to Run a Shell Script
Create the Shell Script:
- Open your favorite text editor (e.g., VSCode, Sublime Text, or even a basic text editor like Notepad).
- Write or paste your shell script. Here’s a simple example for moving selected files:
#!/bin/bash
# Directory containing the files
DIR="{Path}"
# Subfolder to move matching files to
TO_PROCESS_DIR="${DIR}/To Process"
# Ensure the "To Process" directory exists
mkdir -p "$TO_PROCESS_DIR"
# List of substrings to check
substrings=(
06340 06422 06500 06518 06521 06524 06539 06542 06545 06548 06553 06581
06584 06587 06608 06611 06617 06629 06649 06662 06980 06695 06733 06734
06737 06740 06743 06758 06761 06767 06773 06779 06787 06788 06791 06794
06797 06815 06818 06821 06845 06878 06890 06898 06899 06941 06947 06959
06962 06965 06970 06971 06977 06985 06986 07009 07040 07055 07062 07068
07074 07080 07083 07086 07089 07092 07097 07101 07104 07107 07113 07119
07128 07143 07146 07182 07185 07188 07191 07197 07200 07215 07218 07221
07230 07291 07302 07323 07329 07332 07338 07350 07356 07359 07476 07362
07365 07368 07389 07413 07435 07431 07461 07497 07500 07503 07506 07509
07512 07518 07524 07527 07530 07536 07542 07548 07557 07578 07584 07593
)
# Debugging: Check if the directory exists
if [ ! -d "$DIR" ]; then
echo "Directory $DIR does not exist."
exit 1
fi
# Debugging: Check if there are files in the directory
if [ -z "$(ls -A "$DIR")" ]; then
echo "No files found in the directory $DIR."
exit 1
fi
# Loop through all files in the directory
for file in "$DIR"/*; do
filename=$(basename "$file")
echo "Processing file: $filename"
moved=false
for substring in "${substrings[@]}"; do
if [[ "$filename" == *"$substring"* ]]; then
echo "Found substring '$substring' in file: $filename"
# Move the file to the "To Process" directory
mv "$file" "$TO_PROCESS_DIR"
moved=true
break
fi
done
if [ "$moved" = false ]; then
echo "No matching substring found in file: $filename"
fi
done
Save the Script:
- Save the file with a
.sh
extension, for example,move_photos.sh
.
Make the Script Executable:
- Open a terminal window.
- Navigate to the directory where your script is saved using the
cd
command. For example:cd /path/to/your/script
- Make your script executable by running:
chmod +x move_photos.sh
Run the Script:
- Still in the terminal, run your script by typing:
./move_photos.sh
Verify the Results:
- Check the destination directory to ensure that the files have been moved successfully.
Additional Tips:
- Editing the File List: Modify the
FILES
array in the script to include the specific last five digits of the filenames you want to move. - Updating Directories: Ensure that the
SOURCE_DIR
andDEST_DIR
variables are set to the correct paths on your system.
By following these steps, you can efficiently manage and prioritize your photo editing tasks, saving valuable time and effort. This shell script can be customized and expanded to suit various file management needs, making it a versatile tool in your workflow.
How to enable MacFuse/PCloud Drive on Mac Sonoma 14.2.1
I recently upgraded to Mac Sonoma 14.2.1 and MacFuse stopped loading which affected my ability to load PCloud and NTFS drives. I spent a few days trying to troubleshoot everything and in the end it turned out I had to disable Mac System Integrity Protection to get everything to load. I’m sharing in case it helps anyone else.
To disable SIP on your Mac Sonoma for extensions like MacFuse and pCloud Drive:
- Shut down your Mac: Turn off your Mac completely by pressing and holding the power button until the screen goes black.
- Restart in Recovery Mode: Enabling Recovery Mode depends on your Mac. You can try the steps below or follow Apple’s guide here.
Power on your Mac and immediately press and hold Command (⌘) + R to enter Recovery Mode.
Power on your Mac and hold the power button down until Other Options displays. - Open Terminal: In Recovery Mode, select “Utilities” in the macOS Utilities window and select “Terminal.”
- Disable SIP: Type
csrutil disable
in Terminal and press Enter. This disables System Integrity Protection (SIP). - Restart Mac: Click the Apple menu and select “Restart” to reboot your Mac with SIP disabled.
- Re-enable SIP (if needed): Follow the same steps but use
csrutil enable
in Terminal to re-enable SIP.
Note: Disabling SIP poses security risks. Re-enable it once you’ve installed the necessary extensions. Only disable SIP when necessary and understand the potential risks involved.
Installing font awesome pro with bun
After recently switching to bun.sh, I was trying to install Font Awesome Pro. It uses a private registry but their docs have not been updated to support non-npm package managers and bun does not yet support .npmrc files.
You can configure a private registry using an organization scope. First, you must get your auth token from your paid Font Awesome account by going to your account page, scrolling down to the Tokens section, and copying the token.
Copy and paste this string, and replace YOUR_TOKEN_HERE with the token you copied above:
[install.scopes]
"@fortawesome" = { token="YOUR_TOKEN_HERE" , url =
"https://npm.fontawesome.com/"}
Open the terminal and enter these commands:
touch $HOME/.bunfig.toml
nano $HOME/.bunfig.toml
Paste in the config above with your token and then hit CTRL+X to quit, and Y to save when prompted. Now you should be able to run
bun add @fortawesome/fontawesome-pro
How to get website average latency in BASH
I was working on a project today and wanted to be able to get the average latency for an API that I was working on. Performance is a concern because we’re running the API over a VPN, and then SSH tunneling over to another server. I wanted a quick way to do it and wrote a little bash function that will calculate the average for me. I couldn’t find an example on how to do this online so I’m sharing in case anyone else runs into the same issue.
This is tested on Mac only. Add these two functions to your .bashrc and do a shellupdate in terminal to load the latest, or just grab my dotfiles from my github: https://github.com/gregvarghese/dotfiles
curlb(){
curl -s -o /dev/null -w '%{time_starttransfer}\n' "$@"
}
# Usage:
# latencyavg [# of times to run] [URL]
function latencyavg()
{
time=0.00
for (( c=1; c<=$1; c++ ))
do
num1=$(curlb $2 -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,ja;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36' -H 'Connection: keep-alive' --compressed)
echo "$c - $num1"
time=$(echo "$time + $num1" | bc -l)
done
echo "Total Time $time"
echo $(echo $time / $1 | bc -l)
}
Usage example:
latencyavg 20 https://www.godaddy.com
ReportCrash High CPU & How to Disable reportcrash in Mac OSX
For a while now, all of my MacBooks have run extremely hot and the fans have gone nuts. While troubleshooting the issue, Activity Monitor showed that an app named reportcrash has run very high on the CPU and has killed my battery life. Force quitting the app didn’t help as it would start right back up in a few seconds and climb back to 80-100% usage of the CPU.
What is CrashReporter?
CrashReporter runs in any time an application crashes and it’s designed to saves the application state to aid developers in working out why the app crashed. Basically a process is launching, crashing (and invoking CrashReporter) and then re-launching, repeating this cycle never ending.
How to Identify What’s Crashing
To show which process is triggering this cycle and stop it, CrashReporter is pretty verbose in its logging which makes finding the problem app somewhat easier. Open up the console.app (/Applications/Utilities/Console.app) and look towards the end of your system.log to see what app is crashing.
Unfortunately for me, the problem is a driver by some company called EFI and getting the latest drivers didn’t resolve the issue. The next obvious solution was to disable reportcrash.
How to Disable ReportCrash
Fire up terminal and run the following commands to disable reportcrash:
launchctl unload -w /System/Library/LaunchAgents/com.apple.ReportCrash.plist sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.ReportCrash.Root.plist
How to Enable ReportCrash
If you need to reenable crash report, run the following commands in terminal:
launchctl load -w /System/Library/LaunchAgents/com.apple.ReportCrash.plist sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.ReportCrash.Root.plist
How to flatten a directory on Mac through terminal
Recently, I was handed a project with pre-built ANT scripts that zip folders up into individual directories. The zip files in each folder needed to be uploaded to an FTP site without the folder so having to navigate into each was a bit annoying and time consuming for 40 files.
If you find yourself in need of merge or flatten a folder/directory, use this snippet to flatten everything into a single directory. If a file exists with the same name, you’ll be prompted to overwrite or leave in the existing folder.
find $PWD -mindepth 2 -type f -exec mv -i '{}' $PWD/ ';'
In my case, there was an extra file generated within each folder that was not used and I didn’t want to be prompted 40 times to overwrite so changing the -i to -n will force files to be overwritten. I’d recommend using caution with this and only use it when you know the duplicate files don’t matter.
find $PWD -mindepth 2 -type f -exec mv -n '{}' $PWD/ ';'
How to stop CouchDB on Mac OS
I have been working on a proof of concept using CouchDB. After getting the proof of concept working, I spun up a digital ocean droplet and proceeded to setup CouchDB on the server. I went to enable the SSH tunnel so I could access the CouchDB Futon instance and promptly ran into an error because my local instance of CouchDB was using the same port, 5984. I had a lot of trouble finding out an easy way to stop CouchDB because the documentation in the Wiki wasn’t accurate:
sudo launchctl unload \ /usr/local/Library/LaunchDaemons/org.apache.couchdb.plist
As a workaround, in Terminal, run
launchctl list | grep couchdb
This will output something like:
99093 0 org.apache.couchdb.288008
Grab the highlighted portion and run
launchctl stop "org.apache.couchdb.288008"
Substitute the process name accordingly and you should be able to run the ‘launchctl list’ command again and see the process is no longer present.