One way to keep table names dry and only specify them in the Eloquent model when making join queries is by using the getTable() method in the model. This method returns the table name associated with the model.
Here's an example of how you can use it in a join query:
$users = User::join('roles', function ($join) {
$join->on('users.role_id', '=', 'roles.id')
->where('roles.name', '=', 'admin');
})
->get();
In this example, the User model is joined with the roles table. Instead of explicitly specifying the table name, we use the getTable() method to get the table name associated with the User model.
$users = User::join(User::getTable(), function ($join) {
$join->on('users.role_id', '=', 'roles.id')
->where('roles.name', '=', 'admin');
})
->get();
By using User::getTable(), we ensure that the table name is only specified in the model and not repeated in the join query.
I hope this helps! Let me know if you have any further questions.