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.
A sonnet about Batman in the style of William Shakespeare
Oh Batman, knight of darkness, hero bold
Fierce in battle, strong in heart and mind
Defender of the weak and young, behold
Thy righteous justice, keen and unconfined
With cape and cowl, thou roam’st the gloomy night
In search of evil, lurking in the shade
With batarangs and gadgets, swift and right
Thou fight’st with valor, never unafraid
Thou art the Dark Knight, feared by thy foes
But loved by Gotham, city of thy birth
Thy loyal allies, Robin and Batgirl knows
Thy strength of spirit, unmatched on this earth
Oh Batman, thy courage doth inspire
Thy legend shall never fade, nor expire.
AI Experiments Featuring Monkeys
I was experimenting with AI writing and images and was experimenting. My initial testing was for serious subject matters, and it proved to be surprisingly well-written.
I decided to experiment to see how absurd things could get and was not disappointed. Here’s the first result.
A sonnet about monkeys in the style of William Shakespeare
Oh primate of the jungle wild, with fur so soft and warm,
Your funny face is full of cheer, your spirit full of charm.
Your leaps and bounds are so delight, your antics such a sight,
Your antics bring joy and laughter, day and night.
Your antics full of curious play, never a dull moment today,
Your cries bring life to the trees, your presence is here to stay.
Your chatter fills the air with sound, as you swing from tree to tree,
Your kind is full of joy, of this much I guarantee.
Your life is full of family, and you are never lonely,
Your love and care for one another, is so pure and honestly.
Your life is filled with plenty of adventure, curiosity and more,
Your life is full of joy, and near the jungle floor.
Oh primate of the jungle wild, I sit and watch you play,
Your furry face and kind spirit, I shall never forget today.
Your life is full of joy and cheer, your presence warm and bright,
Your life is full of love and fun, and I am glad to be in your sight.