Josh Toth
Josh Toth

Reputation: 764

Proper way to access Database within Laravel 4 Package

I'm relatively new to Laravel, but I think I'm understanding it pretty well. </unintentional-rhyme>

I'm creating a base package for a project, and it needs to interact with a database (which has been set up and functioning - I did a lot of logic in Route closures and am refactoring it).

I had DB::table('shops')->where(...

but it gave me a Class \xxx\xxx\DB not found or whatnot; so I changed it to

\DB::table('shops')->where(... to fix the namespaces.

Is this the proper way to alleviate the issue within a package? I don't want to be causing some sort of issue that comes back to bite me down the road.

Thanks

Upvotes: 0

Views: 55

Answers (2)

chanchal
chanchal

Reputation: 594

If you are using this in controller, so this is the right way to access data

Shop::where('condition', 'value')->get();

check laravel doc http://laravel.com/docs/eloquent

Upvotes: 1

Adam
Adam

Reputation: 1975

You have to declare it at the top like so

<?php

use DB;

class myClass {
    public function foo () {
        DB::insert('insert into users (id, name) values (?, ?)', array(1, 'Dayle'));
    }
}

Upvotes: 1

Related Questions