F
F
fman22019-02-02 15:22:41
Laravel
fman2, 2019-02-02 15:22:41

Laravel without facades. How to use?

I am writing a project in Laravel. After Symfony and Yii2, I'm not comfortable with facades in Laravel, I'm kind of used to using dependency injection. Anyway.
For example, I want to insert a record into the database, without using their AR and DB::table(), I use Illuminate\Database\Query\Builder for this purpose
. I have a simple table for testing from one field: id int
And this one the code:

$this->builder
            ->from("test")
            ->insert([
                'id' => 123
            ]);

Does not work. And when using the facade, everything is OK. Why?
The error is the following:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"test" ("id") values (?)' a
t line 1

Obviously, a crooked SQL query is being created, but because of what? Is it really all so tied to facades?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2019-02-02
@fman2

As a result of clarifications in the comments, it turned out that the builder was obtained from the container in a wrong way. Necessary:

use Illuminate\Database\ConnectionInterface;

public function __construct(ConnectionInterface $conn)
{
    $conn->query()->from(...)->insert();
    // или $conn->table(...)->insert();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question