Use RAW Eloquent Queries with Slim PHP

I am working on a project with SlimPHP and Eloquent. I am trying to run a RAW SQL query in a model method, for example:

/models/Form.php

<?php namespace models; class Form extends \Illuminate\Database\Eloquent\Model { protected $table = 'forms'; public function getResponses($form_id) { // HERE $select = \Illuminate\Support\Facades\DB::select('select 1'); return 1; } } 

I use Capsule to load ORM.

The above code gives me:

Fatal error: calling select () member function for non-object in /vagrant/vendor/illuminate/support/Illuminate/Support/Facades/Facade.php on line 208

The documentation helps a lot in this case, could you shed some light on this?

thanks

+6
source share
2 answers

Read the github setup instructions carefully and make sure you follow them correctly.


With the Capsule you must use the Illuminate\Database\Capsule\Manager or as a DB Facade.

 $select = \Illuminate\Database\Capsule\Manager::select('select 1'); 

I usually import it and define an alias:

 use Illuminate\Database\Capsule\Manager as DB; // ... $select = DB::select('select 1'); 
+8
source

Plus, if you need to execute a raw request, this can help call setAsGlobal () after bootEloquent (), like this

 $capsule->addConnection($sqliteDb); $capsule->bootEloquent(); $capsule->setAsGlobal(); // <--- this 
0
source

Source: https://habr.com/ru/post/984816/


All Articles