0

I'm trying to write a script for laravel that queries my database and fetches tables, but I'm not sure what to include in the file in order to get the DB class to work.

Thus far my code looks like:

$tables = DB::query("SHOW TABLES"); foreach ($tables as $table) { print $table . "\n"; }

I've tried using require "../../vendor/autoload.php" but that doesn't help.

So for instance, the DB class works fine if I call it within a controller, but not if I create a directory, say /application/scripts and create a file test.php . If I try to call DB in that file, the DB class isn't defined (for good reason). I'm wondering what I need to require in that file in order for that class to be defined.

5
  • You want to use the default Laravel Database classes? Commented May 15, 2013 at 16:11
  • Also, check your error logs for issues, it may be that you haven't configured the connection correctly, or that $db in your example is not defined... you need to include more information that you have done for people to assist without to many "guesses" Commented May 15, 2013 at 16:14
  • Sorry I didn't clarify.. I'll edit my post. Commented May 15, 2013 at 16:30
  • Is there any reason this can't be done within the normal MVC structure/namespaces of Laravel (i.e. in a model or the controller)? As Sébastien pointed out below, it's probably not worth the time or effort to do it like this.. Commented May 15, 2013 at 16:46
  • I'm trying to create an automatic class generator that I can run when I update my tables in the database. And I also need this for rewriting some import scripts I have. Commented May 16, 2013 at 1:26

3 Answers 3

3

I would suggest using the Illuminate/Database package (part of Laravel 4). You won't get the DB interface, because that's a special facade provided by the Laravel Framework, but all the query builder functionality is available through the capsule.

Install illuminate/database using composer and then follow the included readme notes (included below for completeness).


Illuminate Database

Usage Outside Of Laravel 4

$config = array(
    'fetch' => PDO::FETCH_CLASS,
    'default' => 'mysql',
    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'laravel',
            'username'  => 'root',
            'password'  => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

$capsule = new Illuminate\Database\Capsule($config);

// If you want to use the Eloquent ORM...
$capsule->bootEloquent();

// Making A Query Builder Call...
$capsule->connection()->table('users')->where('id', 1)->first();

// Making A Schema Builder Call...
$capsule->connection()->schema()->create('users', function($t)
{
    $t->increments('id');
    $t->string('email');
    $t->timestamps();
});
Sign up to request clarification or add additional context in comments.

1 Comment

There's also Capsule which makes this process easier.
1

You do not need to include anything. Laravel will include what it needs and setup the system itself. You are using a variable called $db but it is not defined and means/does nothing. What you are looking for is the DB class. Try this:

$tables = DB::query('SHOW TABLES');
foreach ($tables as $table)
{
    echo $table.PHP_EOL;
}

Also, check out the Fluent DB class documentation: http://laravel.com/docs/database/fluent

1 Comment

that's what I actually had. That's exactly what isn't working inside the script.
0

...Isn't it DB::query()? A call to the database engine is through a class, not a variable, for scoping issues.

2 Comments

If he creates a file which isn't in the Laravel namespace then referencing the DB class still won't work... OP want's to know how to include the Laravel DB class inside a file outside the normal Laravel MVC structure.
That is going to be very difficult and mostly pointless. You'll need to load a very large part of Laravel to get access to the DB wrapper. Might as well just write for L3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.