ThEwHiStLeR
ThEwHiStLeR

Reputation: 45

Eloquent ORM - How to write my own queries?

I am building a slim API and I'm using eloquent ORM. But I would like to have the ability to write my own queries occasionally (for very complex queries).

After researching I thought I could do something like this:

$messages = Capsule::statement("select * from messages");

Note: This is a simple statement just for testing.

But the only thing that is returned is 'true' instead of an array of messages.

Any idea what I am doing wrong?

I have the following in bootstrap.php

include 'config/creds.php';
include 'vendor/autoload.php';

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;
$capsule->addConnection([
    "driver"    => "mysql",
    "host"      => $db_host,
    "database"  => $db_name,
    "username"  => $db_user,
    "password"  => $db_pass,
    "charset"   => "utf8",
    "collation" => "utf8_general_ci",
    "prefix"    => ""
]);

$capsule->bootEloquent();
$capsule->setAsGlobal();

Upvotes: 0

Views: 507

Answers (1)

Yaroslav Dobzhanskij
Yaroslav Dobzhanskij

Reputation: 562

According to documentation in Eloquent's github repo (https://github.com/illuminate/database) Capsule supports all raw query methods, that provides DB facade in Laravel.

They are described here: https://laravel.com/docs/5.4/database#running-queries

According to documentation Capsule::statement() method do not fit your needs. You need to use Capsule::select() method for getting array of query results.

Upvotes: 1

Related Questions