Snippets

Getting the Table Name From a Model in Laravel

Playing
Getting the Table Name From a Model in Laravel
3 mins1 week ago

Sometimes we need to reference table names throughout our codebase, like when we're using the DB facade.

Although table names don't change often, I still get an uneasy feeling hardcoding them. It's so much better to reference the model directly so we have one source of truth for the table name.

So, let's learn how to do that! We'll also create a trait that allows static access to any of our model table names to keep our code a little tidier.

Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.

Suggested courses

Laravel

Build An Uptime Monitor with Inertia

Ready to dive into Inertia? Let's build a real-world app with Laravel, Inertia and Vue! If you're already working with Inertia, you'll pick up some tips and techniques for future projects. This uptime monitor allows you to create and switch between sites, then add endpoints to monitor with frequency intervals. Using the power of scheduling and queues, it'll automatically alert the email addresses you've added via the UI when an endpoint goes down. In this course, you'll learn to: * Build a real app from scratch with Inertia * Work with Laravel queues * Perform actions on models at user-defined intervals * Work with sub-minute schedules in Laravel * Send out channel notifications with Laravel * Use API resources to simplify Inertia data * Organise apps with events and observers * Create modals in Vue * Perform inline editing with Inertia forms

44 episodes
4 hrs 59 mins
Laravel

Build A Static File Blog with Laravel

Let's build a ridiculously fast blog from scratch in Laravel, entirely driven by markdown files. No database required! Your blog will feature everything you're used to, like syntax highlighting, post tagging and pagination. Plus, it'll render in record time. Once you're done with the course, you'll be able to add on additional features with ease, style it up how you want and quickly create new posts since there's no need for an admin panel.

13 episodes
1 hr 11 mins
Laravel

Installing Inertia from Scratch

The Laravel ecosystem gives us packages like Laravel Breeze and Jetstream which come ready set up with Inertia installed. But what if you want to install Inertia from scratch and build your own features on top of it? In this course, whether you're new to Inertia or not, we'll be covering getting Inertia set up on a completely fresh Laravel application. We'll walk through some nice additions like server side rendering, styling with Tailwind and installing Ziggy for named routes. To finish off the course, we'll build a simple app where we can post comments to a timeline, to bring everything nicely together!

14 episodes
1 hr 7 mins
Laravel

Liking Things in Laravel with Redis

Let’s skip the database and build the ability to like any model in Laravel, using Redis. Traditionally you’d reach for the database for this kind of thing, but as you load more models and start performing checks within relationships — things begin to slow down. With a key-value store like Redis, tracking users who have liked comments (or anything) keeps everything ridiculously fast.

6 episodes
21 mins
Laravel

Logging Unique Views in Laravel with Redis

If you need to log unique views in Laravel, you might reach for a database table to track IP addresses or another unique piece of data. Let's take a look at speeding things up both in performance and complexity by using Redis and the HyperLogLog probabilistic data structure. Once we're done, we'll set up a period command to sync views back to the database for easy ordering, and then create a trait to share functionality between other models.

4 episodes
22 mins