I have described the model in the following way:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Feed extends Model
{
protected $collection = 'feeds';
protected $connection = 'mongodb';
protected $appends = ['id'];
protected $hidden = ['_id'];
protected $fillable = ['feed_content'];
public $timestamps = true;
}
I want to save the content of the API response in database logs:
$feed_info = $driver->getAdvertiserFeed();
$feed_id = new Feed;
$feed_id->feed_content = $feed_info;
$feed_id->save();
The connection to MongoDB is configured in this way:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'feeds'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'test'),
'options' => [
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'),
],
],
But when I try to execute the second piece of code, I get this error:
Symfony\Component\Debug\Exception\FatalThrowableError:
Argument 1 passed to Illuminate\Database\Grammar::parameterize()
must be of the type array, string given, called in
/home/pavel/Projects/axonite/vendor/laravel/framework/
src/Illuminate/Database/Query/Grammars/Grammar.php on line 853
at /home/pavel/Projects/axonite/vendor/laravel/framework/src/Illuminate/Database/Grammar.php:138
134| *
135| * @param array $values
136| * @return string
137| */
> 138| public function parameterize(array $values)
139| {
140| return implode(', ', array_map([$this, 'parameter'], $values));
141| }
142|
What exactly do I do wrong? How to fill JSON in a collection in a more correct manner with getting a document id back? I want to save it in the relational database for retrieving it later. Thanks!
Laravel doesn't support noSql database like mongodb and others..
You need to use PHP methods for the specific database or you can use this ORM package: https://github.com/jenssegers/laravel-mongodb
This package will enable all the ORM functionality same as RDBMS.
The answer is that MongoDB configuration was trying to get credentials for Mongo auth from MySQL settings in.env file:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'feeds'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'test'),
'options' => [
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'),
],
],
Replacing it with static values has solved the problem:
'mongodb' => [
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'feeds',
'username' => 'user',
'password' => 'test',
'options' => [
'database' => env('DB_AUTHENTICATION_DATABASE', 'axonite'),
],
],
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.