Mauro Baptista

I'm a software developer! I'm comfortable with PHP and Javascript. Currently developing this blog and a limo reservation app using Laravel and TDD!

Always learning something new!

January 17, 2017 at 00:00 / By Mauro Baptista Laravel PHP

Find Model Using Table Name

On the post about creating a custom validation using data from a different table, I hard-coded the Class Name, since I use the suggested names as standard, but maybe that's not your case.

If you want to find the Model only using the table name, you can use the following code:


public function getModelFromTable($table)
{
    foreach( get_declared_classes() as $class ) {
         if( is_subclass_of( $class, 'Illuminate\Database\Eloquent\Model' ) ) {
            $model = new $class;
            if ($model->getTable() === $table) {
                 return $class;
            }
        }
    }

    return false;
}

This code gets all declared classes and verify which one extends the Model class, then it compares the given table with the Model table.

When it found a match, it returns the class name. This way you need to instantiate the class by yourself!

Whoops, looks like something went wrong.

1/1 ErrorException in Filesystem.php line 111: file_put_contents(): Only 0 of 288 bytes written, possibly out of free disk space

  1. in Filesystem.php line 111
  2. at HandleExceptions->handleError('2', 'file_put_contents(): Only 0 of 288 bytes written, possibly out of free disk space', '/opt/bitnami/apps/carnou.com/blog/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php', '111', array('path' => '/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/7bJ6HI3k3KIUup9xvpOmmFvP6TgkLpEoogx9mBTQ', 'contents' => 'a:4:{s:6:"_token";s:40:"FeoRyUtoZwDpZxEwKotlr1Pym3KAS89NqW9Zg3CX";s:9:"_previous";a:1:{s:3:"url";s:50:"http://carnou.com/post/find-model-using-table-name";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519160033;s:1:"c";i:1519160033;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', 'lock' => true))
  3. at file_put_contents('/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/7bJ6HI3k3KIUup9xvpOmmFvP6TgkLpEoogx9mBTQ', 'a:4:{s:6:"_token";s:40:"FeoRyUtoZwDpZxEwKotlr1Pym3KAS89NqW9Zg3CX";s:9:"_previous";a:1:{s:3:"url";s:50:"http://carnou.com/post/find-model-using-table-name";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519160033;s:1:"c";i:1519160033;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', '2') in Filesystem.php line 111
  4. at Filesystem->put('/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/7bJ6HI3k3KIUup9xvpOmmFvP6TgkLpEoogx9mBTQ', 'a:4:{s:6:"_token";s:40:"FeoRyUtoZwDpZxEwKotlr1Pym3KAS89NqW9Zg3CX";s:9:"_previous";a:1:{s:3:"url";s:50:"http://carnou.com/post/find-model-using-table-name";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519160033;s:1:"c";i:1519160033;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', true) in FileSessionHandler.php line 83
  5. at FileSessionHandler->write('7bJ6HI3k3KIUup9xvpOmmFvP6TgkLpEoogx9mBTQ', 'a:4:{s:6:"_token";s:40:"FeoRyUtoZwDpZxEwKotlr1Pym3KAS89NqW9Zg3CX";s:9:"_previous";a:1:{s:3:"url";s:50:"http://carnou.com/post/find-model-using-table-name";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519160033;s:1:"c";i:1519160033;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}') in Store.php line 263
  6. at Store->save() in StartSession.php line 88
  7. at StartSession->terminate(object(Request), object(Response)) in Kernel.php line 177
  8. at Kernel->terminate(object(Request), object(Response)) in index.php line 56