Reputation: 379
I'm trying to test various query structures and most work.
<?php
namespace App\Controllers;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
class HomeController extends Controller
{
public function index($request, $response)
{
//return var_dump($container);
//$user=$this->db->table('users')->find(1);
//$user=$this->db->table('users')->where('id',1)->get();
//$user=$this->db->table('users')->where('name','alex')->get();
$user=$this->db->select('select * from users where id = ?', array(1));
var_dump($user);
die();
return $this->view->render($response, 'home.twig');
}
}
the queries slashed out are working but the last prior to var dump does not.
the error I am getting is this:
Call to undefined method Illuminate\Database\Capsule\Manager::select()
this is config and caller page:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
// $user = new \App\models\User;
// var_dump($user);
// die();
$app = new \Slim\app([
'settings' => [
'displayErrorDetails' => true,
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'codecourse',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]
],
]);
// Get container
$container = $app->getContainer();
//set illuminate capsule
$capsule = new Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['db']);
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
$container['db'] = function ($container) use ($capsule){
return $capsule;
};
//Register Twig View helper
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig(__DIR__ . '/../resources/views', [
'cache' => false/**'path/to/cache'**/,
]);
// Instantiate and add Slim specific extension
$view->addExtension(new \Slim\Views\TwigExtension(
$container->router,
$container->request->getUri()
));
return $view;
};
$container['HomeController'] = function($container){
return new App\Controllers\HomeController($container);
};
disclaimer this is based on a tutorial I am following, Slim is the very first framework I am learning and I am not sure how to even debug this problem.
Upvotes: 0
Views: 2586
Reputation: 69
Any controller add the following on top
use Illuminate\Database\Capsule\Manager as Capsule;
then in function use like following sql
$alluseraddresses = Capsule::table('useraddresses')->where('user_id', '=', $_SESSION['userid'])->get();
Upvotes: 1
Reputation: 4952
Add and customize this container entry:
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Connection;
use Psr\Container\ContainerInterface as Container;
$container['db'] = function (Container $container) {
$settings = $container->get('settings');
$config = [
'driver' => 'mysql',
'host' => $settings['db']['host'],
'database' => $settings['db']['database'],
'username' => $settings['db']['username'],
'password' => $settings['db']['password'],
'charset' => $settings['db']['charset'],
'collation' => $settings['db']['collation'],
'prefix' => '',
];
$factory = new ConnectionFactory(new \Illuminate\Container\Container());
return $factory->make($config);
};
Then generate and execute a query. Use the get()
method to fetch all rows:
$query = $this->db->table('users');
$query->select('id', 'username', 'email');
$query->where('id', '=', 1);
// fetch all rows (as stdClass)
$rows = $query->get();
// Alternative: fetch all rows as array
$rows = $query->get()->toArray();
var_dump($rows);
Raw Queries with PDO:
// Get the PDO object
$pdo = $this->db->getPdo();
// Use prepared statements
$statement = $pdo->prepare("SELECT * FROM users WHERE id= :id");
$statement->execute(['id' => 1]);
$userRow = $statement->fetch();
Upvotes: 2
Reputation: 9
Use DB; //Top of your page
$user= DB::Select('users')->where('id','=',[$yourIds]);
Upvotes: 0