Playing
01. Getting the Table Name From a Model in Laravel

Episodes

0%
Your progress
  • Total: 3m
  • Played: 0m
  • Remaining: 3m
Join or sign in to track your progress

Transcript

00:00
It's super unlikely that your database table names are ever going to change.
00:04
But whenever I'm referencing these within code, I always like to use the model directly. That means I've just got one source of truth for the table name.
00:13
And if things do ever change around, I don't need to go through my entire code base to update them. Let me give you an example here. So if we're using the database facade, for example,
00:22
to access some data within a table, I might go ahead and hard code in the user's string. Now, that's OK. But if this changes, of course, we're
00:30
going to have to go through every instance of this in our code base and update it. So what's the alternative? Well, the first thing that we can do,
00:37
and let's just die dump this out so we can see it in the browser, is to go ahead and new up a user. So we're going to new up that user model or whichever model we're working with.
00:47
And then we can access the get table method directly on here. So that's pretty much how we do that. But in a minute, I'm going to show you a much better way to do this so we can access this statically.
00:56
So if we open the browser and give that a refresh, yeah, sure enough, we get the user's table out. And wherever we've referenced this, if we update this now model by changing the table name,
01:06
this will always return that new value. OK, so unfortunately, what we can't do is access this statically. So I can't, for example, say get table.
01:15
It's just not going to work. The method doesn't exist. So of course, when we give that a refresh, we're going to get an error.
01:21
So how do we get around this? Well, what we're going to do is just hop over to our user model. And we're going to add in a static method to handle this. So let's go ahead and create out a public static function
01:31
in here called get table name. Of course, we can't give this the same name because a method with get table already exists there. And we can pretty much just do this if we want to.
01:40
So using with, we can chain on with a new static instance of this model. And then we can call the original get table method if we want to.
01:49
Or you can literally just say new static get table name. It's entirely up to you. OK, so now that we've got this, let's just head over to our roots and try this again.
01:58
So now we can say get table name. And yeah, sure enough, that does exist. If we head over, there we go. Much easier.
02:06
But we can take it a little step further by creating out a trait that we can apply to any model that we need to access this on. So let's go back over and change this around.
02:17
So let's head back over to our user model. Pretty much just going to grab this entire method here. And I'm going to go ahead and create out a new directory inside of our models.
02:26
Let's call this traits. And then inside of here, let's go ahead and create out a trait. So let's call this gets static table name. Or gets table name will just about do.
02:39
Let's go ahead and change this to a trait and create that out. OK, so we can just basically paste that method in because it doesn't rely on accessing which model we're working with since we're just
02:49
using with static. And now what we can do is just hop back over to our user model and just use this trait. So let's say gets table name, and we are done.
02:59
Great, so now that we've done that, this is going to work in exactly the same way. Let's hop over to the browser and give that a refresh. Great.
03:07
So like I said at the start, this is highly unlikely to happen. You're not going to really see any of your table names change that often.
03:15
But whenever I'm referencing things like that, it makes me feel a little bit uneasy. So I use this method to reference them wherever I'm referencing a model's table name.

Episode summary

In this episode, we're tackling a simple but useful topic: how to get the underlying database table name from an Eloquent model in Laravel. While your table names probably won't change very often, hardcoding them throughout your app isn't the best idea—it leaves you open to a big headache if you ever do need to change things later.

We start by looking at the most straightforward way to get a model's table name using the getTable() method. The catch? This method is only available on an instance of your model, not the class itself, so it can't be accessed statically. Not great if you want to reference it from lots of places!

To solve this, I walk you through how to add a simple static method on your model that returns the table name. Even better, we take things one step further by moving this static method into a reusable trait, so you can drop this functionality into any of your models, keeping your code DRY and organized.

By the end of the lesson, you'll have a clean, flexible way to get your models' table names—no more hunting through your codebase if a table name ever changes!

Episode discussion

No comments, yet. Be the first!