Answer the question
In order to leave comments, you need to log in
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
]);
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question