4
I am trying to use some classes of the "Laravel structure" in other files, but I am not succeeding.
For example:
I created a file public/teste.php
with the following code:
require __DIR__.'/../bootstrap/autoload.php';
use Illuminate\Support\Facades\DB as DB;
$dbInstance = new DB;
var_dump($dbInstance);
This gives the following return:
Object(Illuminate Support Facades DB)#2 (0) { }
But when I have the following code:
require __DIR__.'/../bootstrap/autoload.php';
use Illuminate\Support\Facades\DB as DB;
$dbInstance = new DB;
var_dump($dbInstance);
$pessoas = DB::table("pessoas")->get();
var_dump($pessoas);
Gives the following result:
Fatal error: Uncaught exception 'RuntimeException' with message 'A facade root has not been set.' in C:\wamp\www\PROJETO\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:210
Stack trace:
#0 C:\wamp\www\PROJETO\public\teste.php(11): Illuminate\Support\Facades\Facade::__callStatic('table', Array)
#1 C:\wamp\www\PROJETO\public\teste.php(11): Illuminate\Support\Facades\DB::table('pessoas')
#2 {main} thrown in C:\wamp\www\PROJETO\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 210
Does anyone have any idea where I’m going wrong?
I would also like to use my Model.
I read the documentation you mentioned and I still can’t fully understand the life cycle of a Portable application. For my problem, Telvez I need to use this class here "Databasemanager", isn’t it also simple used?? My code is not inside the controller because I want to use a Framework I made, and I found it very complicated to incorporate it inside the Controller, so I want to use it from the outside... That’s why I’m trying to access the database from outside the controllers.
– AndersonSouza
In your case you can only use the database component https://github.com/illuminate/database
– gmsantos
@gmsantos seems to me that Laravel, despite being separated, as the very name of the repositories on github says is [Readonly]
– Wallace Maxters
@gmsantos have you managed to use this component there? I’m not getting it.. You are giving the following error: Fatal error: Class 'Illuminate Database Capsule Manager' not found .... I followed the documentation but I was not happy. Like you did?
– AndersonSouza
I got it, it was missing: require .... '/vendor/autoload.php'?
– AndersonSouza
@Wallacemaxters is a split of the main Ref. It does not accept contributions from that Ref, only in the main... so read-only
– gmsantos
@Andersonsouza later I edit the answer.
– gmsantos